简体   繁体   中英

Plotting Frequency Spectrum using Matlab with hold function

Suppose I have successfully generated a Single-sided Power spectrum as follows:

X_mags = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz = bin_vals*fs/N;
N_2 = ceil(N/2);
plot(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)));`

now I want to plot a second graph on top of the first one:

hold on;

Lastly I perform an LPC analysis on the signal and calculate the frequency response. The frequency response should be plotted on top of the Power spectrum, so:

[a, g] = lpc(signal,N);
[h,f] = freqz(b,a,N,fs);
plot(?);

For the sake of simplicity, let's assume the parameters are all correctly given, how should I write the plot function for having a correct display of the frequency response? A simple plot(f) doesn't work.

Can someone explain why? Thanks

The response h is complex, so you'll want to get the magnitude of the response via multiplication by its complex conjugate.:

plot(f, 10*log10(h.*conj(h)));

Note the use of 10*log10 because the operation above squares the amplitude response held in h .

Or, if you're less interested in being pedantic about working with complex numbers, you could just take the absolute value of the complex values, being sure to 20*log10 because the abs does not square the values

plot(f, 20*log10(abs(h)));

A simple plot(f) tries to plot frequency vector, isn' t it?

Check the code below:

X_mags   = abs(fft(signal));
bin_vals = [0 : N-1];
fax_Hz   = bin_vals*fs/N;
N_2      = ceil(N/2);

[a, g]   = lpc(signal,N);
[h, f]   = freqz(b, a, N, fs);

figure,

hold on,
plot(f, 20*log10(abs(h)), 'r');
plot(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)));
title('Frequency Spectrum');
xlabel('Frequency (Hz)'); 
ylabel('Amplitude (dB)');
legend('Frequency Response', 'Single-sided Power spectrum')

Btw, there is a MATLAB function db() for decibel calculation. That might be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM