简体   繁体   English

使用具有保持功能的Matlab绘制频谱图

[英]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. 最后,我对信号进行LPC分析并计算频率响应。 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. 简单的绘图(f)不起作用。

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.: 响应h很复杂,因此您需要通过乘以其复共轭来获得响应的大小。

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

Note the use of 10*log10 because the operation above squares the amplitude response held in h . 请注意使用10 * log10,因为上面的运算将保持在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 或者,如果您对处理复数不太感兴趣,则可以取复数值的绝对值,并确保为20 * log10,因为abs不会使数值平方

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

A simple plot(f) tries to plot frequency vector, isn' t it? 一个简单的plot(f)试图绘制频率矢量,不是吗?

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. 顺便说一句,有一个用于分贝计算的MATLAB函数db() That might be useful. 那可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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