To Get Correct FFT Result the length(x) must be An Integral Multiple of 100?
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
Accepted Answer
More Answers (1)
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
Ziyu Hua
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
Categories
Find more on Smoothing and Denoising 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!
