-1

I'm using MATLAB to plot a recorded sound using the FFT. I want to take the log of the y-axis but I don't know what I did if correct.

Currently, my FFT plotting code looks like this:

nf=1024; %number of point in DTFT   
Y = fft(y,nf);    
f = fs/2*linspace(0,1,nf/2+1);    
plot(f,abs(Y(1:nf/2+1)));    
title('Single-Sided Amplitude Spectrum of y(t)')   
xlabel('Frequency (Hz)')   
ylabel('|Y(f)|')  

What I did is: plot(f,log(Y(1:nf/2+1)));. I replaced abs with log. Is this correct?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Hasan Haj
  • 83
  • 1
  • 7

1 Answers1

1

Applying log on the coefficients itself doesn't make any sense... especially since the spectra will be complex-valued in nature. However, some people usually apply the log on the magnitude of the spectra (hence the abs call) mostly for visualization purposes so that large values of the magnitude don't overwhelm the smaller values. Applying log in this case will allow the larger values to taper off and the spectrum can be visualized easier. but applying the straight log in my opinion isn't correct. The code you have provided plots the magnitude of the single-sided spectrum and so there's no need to change anything.

If you provided more insight as to why you want to use the log, that would be helpful but right now, I would say that the straight log is incorrect. However, if you really must use the log, apply it on the magnitude instead. Also, to prevent undefined behaviour, make sure you add 1 to the magnitude before applying the log so that zero values of your magnitude get mapped to zero, rather than undefined.

As such, do this instead:

nf=1024; %number of point in DTFT   
Y = fft(y,nf);    
f = fs/2*linspace(0,1,nf/2+1);    
plot(f,log(1 + abs(Y(1:nf/2+1)))); %// Change    
title('Single-Sided Amplitude Spectrum of y(t)')   
xlabel('Frequency (Hz)')   
ylabel('|Y(f)|')  
rayryeng
  • 102,964
  • 22
  • 184
  • 193