Kaline Mesquita
Kaline Mesquita

Reputation: 23

How can I filter an audio using butterworth lowpass filter?

I'm trying to filter an audio signal, but I cannot find an IIR filter function in scilab which I can combine with the other function of my code to give an numerator and denominator which I call b2 and a2(2° section).

I tried use the function zpbutt, but I that doesn't worked to me.Also, I tried the function analpf()

//Code to filter a audio called sirene.wav
clc;
clear;
close;
//Load the audio
wavread("C:\Users\kaline\Desktop\Disciplinas\2019.1\PDS\2° etapa\Trabalho final\sirene.wav","size")
[y,fs,bits]=wavread("C:\Users\kaline\Desktop\Disciplinas\2019.1\PDS\2° etapa\Trabalho final\sirene.wav");fs,bits
y=loadwave("C:\Users\kaline\Desktop\Disciplinas\2019.1\PDS\2° etapa\Trabalho final\sirene.wav");
//playsnd(y)
//Cpturing information
Ts = 1/fs;                  // sampling time
t = 0:Ts:1-Ts;              //Interval of the sampling
fa=[0:1:length(y)-1];       //fa scroll the signal samppling
f=fa.*fs/(length(y)-1);     //Frequency vectors in Hertz
X=abs(fft(y));              //frequency spectrum of the audio signal


// Show  plots
subplot(2,1,1);
plot(fa,abs(y));
title('Sinal de Voz');
subplot(2,1,2);
plot(f(1:round(length(y)/2)),X(1:round(length(y)/2)));
title('Espectro do sinal');


//1° section:Notch filter

//hz=iir(n,ftype,fdesign,frq,delta)
//[p,z,g]=iir(n,ftype,fdesign,frq,delta)
r = 0.9926;
fc = 200;
wc = (2*%pi*fc)/fs; 
a = [1 -2*r*cos(wc) r^2]; 
b = [1 -2*cos(wc) 1];
[xm,fr]=frmag(b,a,512);
Y=fft(y);
z=filter(b,a,y);
w=fft(z);

//Coeficientes do primeiro filtro notch
disp('Coeficientes do primeiro filtro notch');
disp(b);
disp(a);

//Apresentando os plots
figure;
title('filtro Notch');
subplot(3,1,1)
plot(fr*fs,abs(xm))
subplot(3,1,2)
plot(f,abs(Y)),
title('Resposta em frequência do sinal original');
subplot(3,1,3)
plot(f,abs(w),'r')
title('Resposta em frequência do sinal filtrado');

//2° section: Butt filter

n = 10;
wc = 0.5;
//[b2, a2] = zpbutt(n,wc); // Parâmetros de entrada

[b2,a2]= iir(n, "lp", "butt",[wc],[]);
[H1, W1]= frmag(b2,a2,512);       //frequency response
s = filter(b2,a2,z);                // Filtering the signal
S = abs(fft(s));                    // frequency response of filtered signal
//[I,T] = impz(b2, a2);             //impulse response of filter IIR

//Coeficientes do filtro de butterworth
disp('Coeficientes do filtro de butterworth');
disp(b2);
disp(a2);

Actually I'm trying to use the function iir who gives me the error:

filter: Wrong type for input argument #1: Real matrix or polynomial expected.

Upvotes: 1

Views: 844

Answers (1)

Felipe Maion
Felipe Maion

Reputation: 351

Try the following:

 //2° section: Butt filter

n = 10;
wc = 0.5;
//[b2, a2] = zpbutt(n,wc); // Parâmetros de entrada

[hz]= iir(n, "lp", "butt",[wc],[])
disp(hz)
a2 = hz.num
b2 = hz.den

[H1, W1]= frmag(a2,b2,512);       //frequency response
s = filter(a2,b2,z);                // Filtering the signal
S = abs(fft(s));                    // frequency response of filtered signal
//[I,T] = impz(b2, a2);             //impulse response of filter IIR

//Coeficientes do filtro de butterworth
disp('Coeficientes do filtro de butterworth');
disp(b2);
disp(a2);

Is this what you want?

Upvotes: 1

Related Questions