To Get Correct FFT Result the length(x) must be An Integral Multiple of 100?
4 views (last 30 days)
Show older comments
Hi, all
I'm trying to get the amplitude- frequency relationship and phase- frenquency relationship using fft. My signal is given by:
t=0:0.005:14; % Time [s] Where mod(length(t),100)~=0
% t=t=0:0.005:14; t(end)=[]; % Where mod(length(t),100)==0
Vs=10*cos(2*pi*20*t-pi/5)+13*cos(2*pi*60*t+pi/6);
Spectrum=fft(Vs)/length(Vs);
Spectrum(2:end-1)=Spectrum(2:end-1)*2;
To get the amplitude and the phase of the signal of specified frequency, I used:
Amplitude=abs(Spectrum);
Phase=angle(Spectrum);
The question is, if the length(Vs) is an integral multiple of 100 (100,2700, etc.), the programe returns a correct amplitude and phase like:
Both Amplitude and Phase value at the DC, 20Hz, 40Hz is the setting value. However, once the length(Vs) is not of an integral multiple of 100, the same program returns a totally wrong result like:
I don't know the reason, and I want a method by which I can get right Spectrum no matter what the length(Vs) is.(On the premise of guaranteeing sampling theorem).
Thanks
0 Comments
Accepted Answer
Paul
on 7 Jul 2021
Edited: Paul
on 8 Jul 2021
The desired plot will be obtained when N, the number of elements of t, is an integer multiple of the ratios Fs/Fn, where Fs is the sample frequency and Fn are the frequencies in Vs (in Hz). In this case we have
Fs = 200;
Fn = [20 60];
Given these vaues, selecting N = k*100 will satisy the criterion:
syms k N
N = k*sym(100);
N*sym(Fn)./sym(Fs) % integer valued for all integer k
Here is the "good" case in the question:
N = 2800;
N.*Fn./Fs % integer
tfinal = func(N,Fs,Fn)
Here is another case, where N satisfies the criteria, but is not a muliple of 100
N = 2820;
N.*Fn./Fs % integer
tfinal = func(N,Fs,Fn)
As you already showed, choosing N = 2801, which does not satisfy the criterion, results in an undesired plot
N = 2801;
N.*Fn./Fs % not both integers
tfinal = func(N,Fs,Fn)
Note this result is not wrong. It is correct based on the input parameters.
function tfinal = func(N,Fs,Fn)
t = (0:(N-1))/Fs;
Vs = 10*cos(2*pi*Fn(1)*t-pi/5)+13*cos(2*pi*Fn(2)*t+pi/6);
Spectrum = fft(Vs)/length(Vs);
Spectrum(2:end-1) = Spectrum(2:end-1)*2;
Amplitude = abs(Spectrum);
Phase = angle(Spectrum);
figure;
stem((0:N-1)/N*Fs,Amplitude)
xlim([0 Fs/2])
xlabel('Hz')
tfinal = t(end);
end
2 Comments
Paul
on 8 Jul 2021
I don't think so. As you said, you don't know exactly what you're looking for (otherwise, you wouldn't have to look for it). All you have is the dat you're given. If even perfectly measuring a pure tone, it's not likely the data will be perfectly matched between N, Fn, and Fs to perfectly figure out the fequency. With N and Fs large enough, you can get pretty close. I think you're touching on basic questions of how signal processing engineers (I'm not one of them) do their art of analyzing data. As is always the case, the more you know about the problem domain, the better off you'll be in selecting an approach to analyze the data and what conclusions to draw from that analysis.
Have a look at some of the examples in the documentation in the Signal Processing Toolbox, such as
doc pspectrum
particularly the sections on "Window Leakage and Resolution" and "More About."
More Answers (1)
LO
on 7 Jul 2021
Edited: LO
on 7 Jul 2021
the fft function will give you values as precise as the sampling rate of the signal. If it is a noisy signal, error can be caused by that too. Your result does not seem "totally wrong", simply "slightly off". The FFT analyzes your data with a sliding window, so you get several points analyzed at a time. Perhaps when the values are "round" then the resuls are rounded as well.
you can try to increse the signal resolution (using a fit fnction - like "polyfit") or you can try smoothen it (using "smooth" or "smoothdata"), to remove some noise. Noise could be filtered out if it is at constant frequency using bandstop filters (see documentation of "filt").
2 Comments
LO
on 7 Jul 2021
Edited: LO
on 7 Jul 2021
I use the FFT function as well and I have no problem with frequencies that are not multiples of 100.
have you tried specifying a fixed length for the FFT analysis ?
Y = fft(X,n);
or use alternatively the pwelch function, get the power as argument, and then find your power spectrum with the equation 10*log10*p
fs = 1000; % sampling rate
nfft= N; % size of FFT window
window= rectwin(nfft); % return a rectangular window
[p,f]= pwelch(x,window,0,nfft,fs); % W/Hz power spectral density within window
PS = 10*log10(p); % your power spectrum
See Also
Categories
Find more on Parametric Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!