Iván Leyton
Iván Leyton

Reputation: 11

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:

    • a lowpass cutting frecuency of 4500hz
    • a bandstop with central fecruency of 3000 hz and bandwith of 1000 hz

and then show this filtered signals in a spectrogam?.

Upvotes: 0

Views: 647

Answers (2)

Matt
Matt

Reputation: 13943

The following Matlab-code should do what you want. 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. 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.

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:

Spectrograms

Upvotes: 1

willpower2727
willpower2727

Reputation: 789

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:

enter image description here

enter image description here

enter image description here

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.

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.

Upvotes: 0

Related Questions