0

I am missing something in the computation of the spectrum of my signal using FFT on Matlab. My code:

%% compute the spectrum of the data (data(t))
L = length(time); % length of the sample
NFFT = 2^(nextpow2(L)-1); % Next power of 2 from length of y
Y = fft(data,NFFT);%/NFFT;%L;
Fs = 1/(mean(time(2:end)-time(1:end-1)));  % compute the sampling frequency
f = Fs/2*linspace(0,1,NFFT/2+1);
loglog(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of My Data')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

Would you be so kind as to tell me where I messed up?

I tried to check if the algorythm works using these two sampling of the same signal (same sampling frequency ; over two different time range 0-10 and 0-100):

fs=1000;
time10 = [0:1/fs:10];
time100 = [0:1/fs:100];
data10 = sin(2*pi*0.23 .*time10)+cos(2*pi*12 .*time10);
data100 = sin(2*pi*0.23 .*time100)+cos(2*pi*12 .*time100);

I guess the two spectrum should supperpose but they don't... As seen here: https://www.dropbox.com/s/wfols9o409pr94u/FFT_spectrum_StackOverflow.png?dl=0 https://www.dropbox.com/s/a8vmzwto6x4130w/FFT_spectrum_StackOverflow.fig?dl=0

Thanks

LeChat
  • 466
  • 3
  • 18
  • 1
    For the first example, you can explain why do you think this isn't working (show a log error or a photo). And for the second example you can upload a photo which show that the both spctrum doesn't supperpose at all. – Théo P. Jul 26 '17 at 14:25
  • 1
    And for the second example, the both spectrum must not supperpose because the rectangular window you're implicitly isn't the same in both `data`. – Théo P. Jul 26 '17 at 14:29
  • 1
    This code is more or less the same as what MathWorks provides. I don't see where you "screwed up". Can you explain why you think this is not correct? – rayryeng Jul 26 '17 at 14:34
  • There is only one example. The first part of the code is the actual algorythm, the second paragraph of code is just the signals I used to probe the (first) code. Basically, these two signals are in fact the same function computed with the sample sampling frequency fs but over differents time range (10 and 100). I should therefore find the same spectrum for both signal. This is not the case as seen here: https://www.dropbox.com/s/a8vmzwto6x4130w/FFT_spectrum_StackOverflow.fig?dl=0 (.fig) ; https://www.dropbox.com/s/wfols9o409pr94u/FFT_spectrum_StackOverflow.png?dl=0 (.png) Thanks for your help – LeChat Jul 26 '17 at 15:07

1 Answers1

2

The good news is that there is nothing wrong with your computation of the spectrum by itself.

The problem is that by looking at samples of different lengths you are effectively looking at two different samples altogether. In the time-domain, they can be seen as the result of a multiplication of an infinitely long sinusoidal with a rectangular window of different lengths.

In the frequency-domain, the spectrum of the infinitely long continuous-time sinusoidal signal gets convoluted with the spectrum of the rectangular windows. With different window length the corresponding spectrum of those windows have different width (narrower spectrum for longer rectangular windows). As a result, the spikes in the spectrum of the infinitely long sinusoidal signal would get spread over different bandwidths. This is exactly what you are seeing.

SleuthEye
  • 14,379
  • 2
  • 32
  • 61