How do I plot a phase plot for FFT and code time history for down-sampled signal?

Hello,
Thus far, i have completed this code which gave me a frequency vs amplitude graph. I'm ussure how to get a phase graph from this or time history of down sampled signal by a given rate etc I nwould be grateful for assistance for this.
data = [0 0.829668000000000
0.00833300000000000 0.829801000000000
0.0166670000000000 0.828810000000000
0.0250000000000000 0.829008000000000
0.0333330000000000 0.828352000000000
0.0416670000000000 0.827073000000000
0.0500000000000000 0.826807000000000
0.0583330000000000 0.826828000000000
0.0666670000000000 0.826857000000000
0.0750000000000000 0.827056000000000
0.0833330000000000 0.827139000000000
0.0916670000000000 0.828689000000000]
data = 12×2
0 0.8297 0.0083 0.8298 0.0167 0.8288 0.0250 0.8290 0.0333 0.8284 0.0417 0.8271 0.0500 0.8268 0.0583 0.8268 0.0667 0.8269 0.0750 0.8271
t = data(:,1);
s = data(:,2);
Ts= 0.0083;
Fs=1/Ts;
Fn= Fs/2;
L=4000;
smean=mean(s);
Fts=fft(s-smean)/L;
Fv= linspace(0,1,fix(L/2)+1)*Fn;
Iv=1:numel(Fv);
figure
plot(Fv,abs(Fts(Iv))*2)
Index exceeds the number of array elements. Index must not exceed 12.
grid
xlabel('frequency')
ylabel('Amplitude')

 采纳的回答

Perhaps this —
data = [0 0.829668000000000
0.00833300000000000 0.829801000000000
0.0166670000000000 0.828810000000000
0.0250000000000000 0.829008000000000
0.0333330000000000 0.828352000000000
0.0416670000000000 0.827073000000000
0.0500000000000000 0.826807000000000
0.0583330000000000 0.826828000000000
0.0666670000000000 0.826857000000000
0.0750000000000000 0.827056000000000
0.0833330000000000 0.827139000000000
0.0916670000000000 0.828689000000000]
data = 12×2
0 0.8297 0.0083 0.8298 0.0167 0.8288 0.0250 0.8290 0.0333 0.8284 0.0417 0.8271 0.0500 0.8268 0.0583 0.8268 0.0667 0.8269 0.0750 0.8271
t = data(:,1);
s = data(:,2);
Ts= 0.0083;
Fs=1/Ts;
Fn= Fs/2;
L=size(data,1);
smean=mean(s);
NFFT = 2^nextpow2(L);
FTs=fft((s-smean).*hann(L))/L;
Fv= linspace(0,1,NFFT/2+1)*Fn;
Iv=1:numel(Fv);
figure
subplot(2,1,1)
plot(Fv,abs(FTs(Iv))*2)
grid
ylabel('Amplitude')
subplot(2,1,2)
plot(Fv,rad2deg(angle(FTs(Iv))))
grid
xlabel('Frequency')
ylabel('Phase (°)')
Also consider using unwrap for the radian phase, as: ‘rad2deg(unwrap(angle(FTs(Iv))))’. (I made a few improvements in my original code.)
.

4 个评论

Hello,
Thanks, I have got the phase plot . For ' showing time history of the signal downsampled for example to fs/5' I changed the fs value in above code but I am unsure what else this requires/means
I've included some example codes for this.
time history of a signal down-sampled under the rates of 25Hz & 40Hz and find its DFT analysis using Matlab
Fs = 1000; % sampling rate
T=1/Fs; % sample time
L= 20000; % lenght of signal
t=(0:L-1)*T; % time vector
figure(1)
plot(t,u)
grid;
title('Time History of the Signal')
% dft analysis
yf=fft(u);
w=[0:2*pi/L:(L-1)*2*pi/L];
figure(2)
plot(w/(2*pi*T),abs(yf))
grid
title('DFT analysis of the signal sampled at 1000Hz')
Example 2 for a function , but not for my data set as desired.
x(t) = cos ( 10 t) + e (t) , t <10 sec fs= 512 hz
fs=512; dt=1/fs;
t=0:dt:10;
N=length(t);
e=0.5*(rand(1,N)-0.5)
x=cos(10*t)+e;
lpFilt = designfilt('lowpassiir','FilterOrder',20, ...
'PassbandFrequency',15/(2*pi),'PassbandRipple',0.01, ...
'SampleRate',fs);
y=filter(lpFilt,x);
figure(1)
w=[0:2*pi/N:2*pi*(N-1)/N]
X=abs(fft(x)/(N/2));
plot(w,X)
hold on
xlim([0,0.1*pi])
figure(2)
Y=abs(fft(y)/(N/2));
plot(w,Y)
hold on
xlim([0,0.1*pi])
figure(3)
plot(t,x)
hold on
plot(t,y)
I am not certain what you mean by ‘time history’. That is generally shown by plotting the signal as a function of time, that being a time-domain plot.
If you want the frequency as a function of time, use the pspectrum function with the 'spectrogram' option. (I prefer pspectrum to spectrogram because the units are easier to interpret.)
The pspectrum function does not display phase. I have never displayed a phase plot as a function of time, although that could be possible by creating a matrix consisting of phase calculation vectors over different time windows corresponding to the pspectrum time windows, and then using surf to plot the resulting matrix. They would have to be displayed on different axes.
Also, use filtfilt, not filter, to do the actual filtering.
Thanks for assisting with your obvious expertise!
This is my first time using matlab and for filtering I came across a simple code for our case above but it displayed error message
inputSignal = (t, s);
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters
F = 8
Fs = 30
[y, x] = butter(5, F/(Fs/2), 'low’)
inputSignal = (t, s);
outSignal = filter(y, x, inputSignal);
plot(outSignal)
As always, my pleasure!
I would do this instead —
F = 8
Fs = 30
[y, x] = butter(5, F/(Fs/2), 'low’)
inputSignal = s;
outSignal = filtfilt(y, x, inputSignal);
figure
plot(t, outSignal)
There is no reason to include the time vector in the signal being filtered, so I eliminated it here.
However if you want to concatenate two column vectors, use square brackets as [a, b] (although the comma is optional) and if you want to concatenate two row vectors [a; b] using the semicolon to create a second row (to vertically concatenate them). In all instances, the vectos must have the same row and column dimensions.
Actually, I would design the filter differently (I prefer elliptic filters because they are more computationally efficient), however to design a Butterworth filter I would begin with the buttord function, then butter, however producing zero-pole-gain output, and then use the zp2sos function to produce a second-order-section representation, and call filtfilt as:
outSignal = filtfilt(sos, g, inputSignal);
The filtering will be more effective and the filter will always be stable with these changes.
.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by