简体   繁体   English

Matlab频谱图,黄油和过滤器功能问题

[英]Matlab spectrogram, butter and filter functions questions

  1. how can i change the colors of the spectrogram to show more intense near to violet and less intense near to red? 如何更改光谱图的颜色,使其在紫罗兰色附近显示更强烈而在红光附近显示更少?

  2. How can i apply the butter and filter functions to the wav file to show: 我如何将黄油和过滤器功能应用于wav文件以显示:

    • a lowpass cutting frecuency of 4500hz 4500hz的低通切割频率
    • a bandstop with central fecruency of 3000 hz and bandwith of 1000 hz 具有3000 hz的中心频率和1000 hz的带宽的bandstop

and then show this filtered signals in a spectrogam?. 然后在分光镜中显示此滤波后的信号?

The following Matlab-code should do what you want. 下面的Matlab代码应做您想要的。 The arguments for the filter-design are defined as variables and can be changed to adjust the filter. 过滤器设计的参数定义为变量,可以更改以调整过滤器。 If you want the plots to be separated, just erase subplot(3,1,x) and put figure before spectrogram . 如果要分离图,只需擦除subplot(3,1,x)并将figure放在spectrogram figure之前。 Then you'll have three individual plots. 然后,您将获得三个单独的地块。

To use your .wav -file, delete the line which loads the sample data and uncomment the line with the audioread -command. 要使用.wav -file,请删除加载样本数据的行,并使用audioread -command取消注释该行。

load('speech_dft.mat');         % sample Matlab data
%[y,fs] = audioread(hfile);      % read wav-file

% Define a custom colormap
cmap = [0.4160    0.0350    0.0350;
        0.5620    0.0260    0.0260;
        0.7080    0.0180    0.0180;
        0.7810    0.0130    0.0130;
        0.9270    0.0040    0.0040;
        1.0000         0         0;
        1.0000    0.1410         0;
        1.0000    0.2120         0;
        1.0000    0.3530         0;
        1.0000    0.3880         0;
        1.0000    0.5290         0;
        1.0000    0.5650         0;
        0.9790    0.5480    0.1120;
        0.9570    0.4950    0.2240;
        0.9250    0.4170    0.3920;
        0.9040    0.3650    0.5040;
        0.8710    0.2860    0.6710;
        0.8130    0.2040    0.8160;
        0.7860    0.2010    0.7930;
        0.7060    0.1910    0.7240;
        0.5990    0.1770    0.6320;
        0.4390    0.1570    0.4940];

% Define nfft, window and noverlap
nfft     = 256;
window   = hanning(nfft);
noverlap = round(nfft/2);

% Display the spectrogram of the unfiltered signal
figure;
subplot(3,1,1);
spectrogram(y,window,noverlap,nfft,fs,'yaxis');
colormap(cmap);
title('Unfiltered signal');

% Design and apply the lowpass filter
order = 4;
fg    = 4500;
[b,a] = butter(order,fg/fs/2,'low');             % design filter
x1    = filter(b,a,y);                           % apply filter

% Display the spectrogram of the lowpass filtered signal
subplot(3,1,2);
spectrogram(x1,window,noverlap,nfft,fs,'yaxis');
colormap(cmap);
title('Lowpass filter');

% Design and apply the bandpass filter
order   = 10;
lowfreq = 2000;
hifreq  = 4000;
[b,a]   = butter(order,[lowfreq,hifreq]/(fs/2), 'bandpass'); % design filter
x2      = filter(b,a,y);                                     % apply filter

% Display the spectrogram of the bandpass filtered signal
subplot(3,1,3);
spectrogram(x2,window,noverlap,nfft,fs,'yaxis');
colormap(cmap);
title('Bandpass filter');

It produces the following result: 它产生以下结果:

频谱图

Ok I'm going to give a better example of what is discussed in the comments section. 好的,我将给出一个更好的示例,说明在评论部分中讨论的内容。 I hope you find this useful. 希望这个对你有帮助。 Basically there is a noisy sinusoid signal. 基本上有一个嘈杂的正弦信号。 I make a spectrogram, and then filter some of the noise away and make another spectrogram to show the result of filtering. 我制作了一个频谱图,然后滤除了一些噪声,然后制作了另一个频谱图以显示过滤的结果。

x = 0:0.001:4*pi;
y = sin(x);
y2 = wgn(1,length(x),0.5);
y3 = sin(314*x);
y4 = y+y3+y2;

figure(1)
plot(x,y4);

nfft = 2^nextpow2(length(y4));
[S,F,T,P] = spectrogram(y4,250,50,nfft,1000);

figure(2)
spectrogram(y4,250,50,nfft,1000);

%create filter and use it on the noisy signal
[b,a] = butter(4,0.5,'low');
y5 = filtfilt(b,a,y4);

figure(3)
spectrogram(y5,250,50,nfft,1000);

Here are the plots generated: 这是生成的图:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

As you can see the spectrogram call has a default scaling of the colors used, you could alter the colors through the figure handle, which if you want to know how to do that I'd ask another question on SO or google it. 如您所见,光谱图调用具有默认使用的颜色缩放比例,您可以通过图形手柄更改颜色,如果您想知道该怎么做,我会在SO或Google上问另一个问题。

And please heed the advice of Matt in the comments section who warns that you need to be certain of how you want to filter the wav file. 并且请注意评论部分中Matt的建议,该警告警告您需要确定要如何过滤wav文件。

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

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