I want to use the sinc function to interpolate some data I already have sampled. I am not very familiar with Matlab, but I found the following script:
rng default
t = 1:10;
x = randn(size(t))';
ts = linspace(-5,15,600);
[Ts,T] = ndgrid(ts,t);
y = sinc(Ts - T)*x;
plot(t,x,'o',ts,y)
xlabel Time, ylabel Signal
legend('Sampled','Interpolated','Location','SouthWest')
legend boxoff
I understand how to change the formatting.
My question is, how can I do this for my own sampled data?
Any help would be greatly appreciated. Thank you very much!
Edit: I tried replacing the x-values with my own multiple times, but each time it reverts to the original when I run the code. I'm not sure why this happens...
Related
So I have 2 sine waves, one is the input to a system, the other being the output of the anti-aliasing bessel filter on the output of the system. There is a slight phase difference, so I've been using Matlab to work out the difference between the two waves. I then plot them both to compare them - however I get this:
As you can see, the input wave is a sine wave, however the output wave appears to be being quantised. In comparison, here's the two waves before I take the phase difference out:
As you can see the Bessel output is also a smooth sine wave.
Here's the code I'm using to plot the first graph:
yyaxis left
plot(Time(1:length(s1a1)),CIN(1:length(s1a1)))
ylabel('CIN')
yyaxis right
plot(Time(1:length(s1a1)),Bessel(-lagDiff+1:end))
ylabel('Bessel Out')
xlabel('Time')
title('CIN vs Bessel')
And here's the code for the second:
yyaxis left
plot(Time,CIN)
ylabel('CIN')
yyaxis right
plot(Time,Bessel)
ylabel('Bessel Out')
xlabel('Time')
title('CIN vs Bessel')
I'm using the exact same variables, the only difference is instead of using the whole length of the data I'm using only a portion. The weird thing is that this works fine for CIN - it's perfectly normal, only when I do it for Bessel does it do this weird quantisation thing.
Would appreciate if I could get a solution - I've already tried interp1 function with spline as the method, and that doesn't change anything. As well as that, I need to use this data elsewhere outside of MATLAB so I can't really alter it.
Any help would be greatly appreciated.
Some edits for MCV Example.
I have reproduced my input wave and output wave roughly using these functions:
Time = 0:0.00002:4;
Bessel = 0.1+(sin(((2*pi*1).*Time)-1)).*0.05;
CIN = 4400+(sin((2*pi*1).*Time).*4000);
L = length(Bessel);
L = L/20;
L=round(L);
Value = Bessel(L);
Bessel(1:L) = linspace(0,Value,L);
Code being used to remove phase shift is:
Fs = 50000;
t1 = (0:length(Bessel)-1)/Fs;
t2 = (0:length(CIN)-1)/Fs;
[acor,lag] = xcorr(CIN,Bessel);
[~,I] = max(abs(acor));
lagDiff = lag(I);
timeDiff = lagDiff/Fs;
s1a1 = Bessel(-lagDiff+1:end);
t1a1 = (0:length(s1a1)-1)/Fs;
s2a2 = CIN(1:length(s1a1));
When I then run it through my code I get the following:
It works for these waves, using the exact same code. But the only change I have made is the input wave, which as established above in my 2nd figure, the input wave is not an issue, it's only when I shift it, as in my 1st figure.
I don't know what the issue is, but it's definitely not the input wave.
I am currently designing a 5th order Butterworth filter and looking at its transfer function response in Matlab. I have successfully calculated it and have plotted its bode response like this:
% Butterworth Fifth Order Low Pass
figure(1)
h = bodeplot(FinalTF);
setoptions(h,'FreqUnits','Hz','PhaseVisible','off');
title('Butterworth LowPass Fifth Order');
grid on;
where FinalTF is the transfer function I'm talking about. What I want is to add markers on specific points in this plot (specifically I want to highlight the frequencies fp,fo,fs, you don't need to know what these are, they're just 3 different points on the x-axis, and the dB at each frequency) with code. I know how to do it by clicking on the graph, but that will be too time consuming, as I have many plots to go through. I am currently running into two basic problems:
1) I don't know how to get the specific dB at each frequency just by using the TF object. I tried using the function evalfr(), but tbh the values it returns seem a bit off.
2) Ignoring the previous point, even if I do the calculations by hand, I can't add them on the plot using this method, and I'm not sure what the problem is. Maybe because I'm using bodeplot instead of regular plot? I don't know how else to do it though.
I'm using Matlab 2015, if it makes any difference.
Any help would be appreciated. Thanks in advance.
I actually found a solution. Here is some sample code to illustrate the results. Before I was doing:
figure(3);
h = bodeplot(FinalTF);
setoptions(h,'FreqUnits','Hz','PhaseVisible','off');
grid on;
title('Chebyshev 2nd Order Band Pass');
It just printed the bodeplot of the transfer function. Now I managed to add markers to the specific frequencies I wanted like this:
figure(4);
myW = logspace(1,5,1000);
myW = 2*pi*myW;
[mag,~,wout] = bode(FinalTF,myW);
mag = squeeze(mag);
wout = squeeze(wout);
mag = 20*log10(mag);
wout = wout/2/pi;
semilogx(wout,mag,'-b');
axis([min(wout) max(wout) min(mag)-10 max(mag)+10]);
title('Chebyshev 2nd Order Band Pass');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
hold on;
freqMarkers = [w0 w1 w2 w3 w4];
[dbMarks,~,freqMarks] = bode(FinalTF,freqMarkers);
dbMarks = squeeze(dbMarks);
freqMarks = squeeze(freqMarks);
dbMarks = 20*log10(dbMarks);
freqMarks = freqMarks/2/pi;
semilogx(freqMarks,dbMarks,'r*');
And works great! Thanks to #Erik for the help.
You can use [mag,~,wout] = bode(sys) and then plot(wout,mag) to create the Bode plot. Then, using hold on and plot(...), you can add whatever points you need to the plot.
Note that wout is in radians per TimeUnit, which is a property of sys (source). To convert wout to a frequency axis in Hertz, you can set TimeUnit in sys using sys.TimeUnit = 'seconds' (which is the default, so probably unnecessary) and then f = wout/2/pi;. Plot it using plot(f,mag), then hold on and plot your markers.
To calculate the magnitude at certain frequencies, use mag = bode(sys,w); where w are the frequencies in radians per sys.TimeUnit. If sys.TimeUnit is 'seconds' and you frequencies are in Hertz, use w = 2*pi*f, where f are the frequencies you need.
I am trying to create a plot using the below code, the plot it produces incorporates values of the x-axis from 0.5-1, however I need the plot to not incorporate this part on the x-axis, from values 0.5-1. I assumed that lines 9 & 10 of the code would exclude this part of the plot, however this is not the case.
Does anyone know what I would write for this part of the plot to be excluded. Thank you!
x = [0:0.01:1];
y = [0:0.01:1];
z = size(101,101);
for j = 1:101;
for k = 1:101;
z(j,k)=((x(j))./(0.5-y(k)));
if z(j,k)>1
z(j,k)=1;
elseif z(j,k)<0
z(j,k)=1;
end
end
end
surf(x,y,z)
xlim([0 .5]) sets the range to be displayed on the x axis. Use it after your plot command (surf).
x=[0:0.005:0.5];
This would exclude the data you do not want to see in the plot, but would give you better resolution for your answer.
MWc answer would work as well, but would just exclude the values from 0.5 on.
So it depends on exactly what you wanted to see given your overall problem.
I am very beginner for matlab and try to solve this question. But so far it is not successful. I have spent quite a time and I think I need some help. I would appreciate any help!!!
I need to plot v against time and trajectory of v and w in phase space. The whole question is below and my code for previous question connected to this question is also below. I can go with subplot(2,1,1) for the first graph and subplot(2,1,2) for the next graph. But I am not sure what I have to do other than this. I kind of found ode45 command. But not sure how to use it and if it is the right one to use here. I have tried to use ode45. But it shows many errors that I don't understand.....Please help me on this. Thanks a lot!
'Create a figure which contains two graphs, using subplot. In the first graph, plot the temporal evolution of the membrane potential v(t) against time t. In the second graph, plot the corresponding trajectory (v(t); w (t)) in (the so-called) phase space.'
% my code is below.
a=0.08;
b=3.6;
c=0.7;
T=2; % this can be any number
I_ext=20; % this can be any number
dt=0.01; % this can be any number
function [ v,w ] = fhnn( a,b,c,I_ext,T,dt)
v=zeros(1,numel(T/dt));
w=zeros(1,numel(T/dt));
for n=1:numel(T/dt)
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
I gather you have a differential equation and are trying to directly plot that. You might find a better approach would be to actually solve the equation if that is possible.
Either way, recognising that:
numel returns the length of an array and dT/dt is always a scalar so the length is always one.
fhnn is not used here.
You still need a vector t.
If what is in your for loop is correct, the following should work:
a=0.08; b=3.6; c=0.7; T=2; I_ext=20; dt=0.01;
t = 0:dt:T;
v = zeros(1,round(T/dt));
w = zeros(1,round(T/dt));
for n=1:round(T/dt)-1
v(n+1)=(v(n)-v(n)^3/3-w(n)+I_ext)*dt+v(n);
w(n+1)=a*(v(n)-b*w(n)+c)*dt+w(n);
end
subplot(2,1,1)
plot(t,v)
xlabel('t')
ylabel('v')
subplot(2,1,2)
plot(v,w)
xlabel('v')
ylabel('w')
I'm having trouble calculating a series on MATLAB. I'm trying to use the symbolic toolbox, but MATLAB keeps busy and never returns my result. This is my code:
function Y = ProbCollision_S (n,tp,s,T)
syms k;
format long;
Y = double(symsum(exp(-n.*s./T).*((n.*s./T).^k).*(1-(1-k.*tp./s).^k)./factorial(k),2,Inf));
end
Please help?
Thank you in advance.