I've trouble with plotting a correct FFT Amplitude Spectrum signal in decibels and Hertz.
Firstly, I just plot the FFT Spectrum signal in Hz and magnitude like this:
figure; X_mags = abs(fft(signal)); bin_vals = [0 : N-1]; freq_ax_bins = bin_vals*fs/N; N_2 = ceil(N/2); plot(freq_ax_bins(1:N_2), X_mags(1:N_2)); title('FFT Spectrum signal 1'); xlabel('Frequency (Hz)') ylabel('Magnitude');
This results in the expected plot with magnitude always > 0. Finally, I just want to do the same but in decibels:
bin_vals = [0 : N-1]; freq_ax_Hz = bin_vals*fs/N; N_2 = ceil(N/2); figure; plot(freq_ax_Hz(1:N_2), 10*log10(X_mags(1:N_2))); xlabel('Frequency (Hz)') ylabel('Amplitude (dB)');
It looks good but the plot is partially drawn into minus dB. Can someone show me how to correctly draw the spectrum in decibels?matlabfft
For your first plot, I notice you're only plotting the first half of the signal. Another, maybe easier way to do this is with
>> Xmag = fftshift(abs(fft(x)));
Now the DC value is in the middle rather than at the beginning. The frequency vector is
(-N/2:N/2 - 1)*fs/N
If your signal is not symmetric, then you need to look at the negative values.
For the second, note that dB requires
20*log10 unless you take the square of
Xmag. Not a big deal, just a scalar, but thought you'd want to know.
Also, minus dB are defined and expected. The
log(-1) is not defined for real numbers, but
log(.1) or any other number less than 1 returns a negative answer.
Hope this helps!