I understand that spec will give me the frequency and the corresponding amplitude of that component, whereas fft will compute the DFT of the signal and throw the complex numbers for each component which I have to plot as per the physical frequencies.

y is the time-series signal Fs is the sampling freq.

N = length(y);
fa = seq(0,N-1,by=1)*(Fs/N)
plot(fa,abs(f1),type='l',xlim = c(0,3),ylim = c(0,50))
s1=spec(y,f=1000,plot = FALSE)
s1$x<-s1$x*1000   (KHz to Hz)
plot(s1$x,s1$y,type='l',xlim = c(0,3))

Red one is from spec function and blue is from fft function.

enter image description here

  • $\begingroup$ FFT is a low level function to compute the raw complex valued DFT of the given data untouched. SPEC function seems to pre and post-process the FFT results (as @Florian indicated). Hence they would produce different (but nevertheless related) outputs. $\endgroup$ – Fat32 Jul 26 '19 at 12:12

Disclaimer: I don't know even a bit of R. But I checked the documentation of the spec function: A Hanning function is applied to the analysis window.

This may very well be the source of the difference in the two plots. The fft function does not apply any window (i.e., it's only the natural rectangular window we are seeing).

| improve this answer | |
  • $\begingroup$ Thanks! It makes sense. I am now trying to figure out what could be the best windowing function to apply. Trying to extract respiratory waveform from an accelerometer attached to the chest. I have noise around 0.1 Hz and some high-frequency noises. Freq of interest is around 0.1 to 2 Hz. $\endgroup$ – Rutuja Chhajed Jul 30 '19 at 6:45
  • $\begingroup$ Apart from the strong DC component, I don't understand this peak at around 0.1 Hz. $\endgroup$ – Rutuja Chhajed Jul 30 '19 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.