Part B
clear all
clc
fm=1000; fl=710; fh=1420;
Gl=0.25;
Gm=1;
Gh=4;
[x fs]=audioread(‘/Users/hanboyu/Desktop/Music.wav’);
wcl=2*pi*fl/fs;
wcm=2*pi*fm/fs;
wch=2*pi*fh/fs;
B=wch-wcl;
N=length(x);
y=zeros(N,1);
z=zeros(N,1);
v=zeros(N,1);
for n=2:N
y(n)=((Gl*tan(wcl/2)+sqrt(Gl))*x(n)+(Gl*tan(wcl/2)-sqrt(Gl))*x(n
1)-((tan(wcl/2)-sqrt(Gl))*y(n-1)))/(tan(wcl/2)+sqrt(Gl));
end
for n=3:N
z(n)=((sqrt(Gm)+Gm*tan(B/2))*y(n)-(2*sqrt(Gm)*cos(wcm))*y(n
1)+(sqrt(Gm)-Gm*tan(B/2))*y(n-2)+(2*sqrt(Gm)*cos(wcm))*z(n-1)
(sqrt(Gm)-tan(B/2))*z(n-2))/(sqrt(Gm)+tan(B/2));
end
for n=2:N
v(n)=((sqrt(Gh)*tan(wch/2)+Gh)*z(n)+(sqrt(Gh)*tan(wch/2)
Gh)*z(n-1)-(Gh*tan(wch/2)-1)*v(n-1))/(Gh*tan(wch/2)+1);
end
sound(v,fs);
Matlab code of 2nd order filter design
clc;
clear;
fs = 24000; % sampling frequency
fl = 710; % lower frequency
fc = 1000; % center frequency
fu = 1420; % upper frequency
[x fs] =
audioread(‘Users\Dechen\Documents\MATLAB\ELEN90058\Week7\Mu
sic.wav’);
N = length(x);
y = zeros(N,1);
z = zeros(N,1);
s = zeros(N,1);
% parameters in the 2nd order low shelving filter
O1 = tan(pi*fl./fs);
G1 = 0.25;
K1 = sqrt(G1);
A1 = K1*O1^2 + O1*sqrt(2*K1) + 1;
B1 = 2*(K1 * O1^2 -1);
C1 = K1*O1^2 – O1*sqrt(2*K1) + 1;
D1 = K1 + O1*sqrt(2*K1) + O1^2;
E1 = 2*(O1^2 – K1);
F1 = K1 – O1*sqrt(2*K1) + O1^2;
% parameters in the 2nd order high shelving filter
O3 = tan(pi*fu./fs);
G3 = 4;
K3 = sqrt(G3);
A3 = K3 + O3*sqrt(2*K3) + O3^2;
B3 = 2*(O3^2 – K3);
C3 = K3 – O3*sqrt(2*K3) + O3^2;
D3 = K3*O3^2 + O3*sqrt(2*K3) + 1;
E3 = 2*(K3 * O3^2 -1);
F3 = K3*O3^2 – O3*sqrt(2*K3) + 1;
% parameters in the 2nd order peaking filter
B = 2*pi*(fu-fl)./fs;
w2 = 2*pi*fc./fs;
G2 = 1;
K2 = sqrt(G2);
A2 = K2 + G2*tan(B/2);
B2 = -2*K2*cos(w2);
C2 = K2 – G2*tan(B/2);
D2 = K2 + tan(B/2);
E2 = -2*K2*cos(w2);
F2 = K2 – tan(B/2);
% 3 filters in cascade
for i=3:N
y(i) = (A1*K1*x(i) + B1*K1*x(i-1) + C1*K1*x(i-2) – E1*y(i-1) –
F1*y(i-2))/D1;
end
for i=4:N
z(i) = (A2*y(i) + B2*y(i-1) + C2*y(i-2) – E2*z(i-1) – F2*z(i-2))/D2;
end
for i=3:N
s(i) = (A3*K3*z(i) + B3*K3*z(i-1) + C3*K3*z(i-2) – E3*s(i-1) –
F3*s(i-2))/D3;
end
sound(s, fs);
C code of 1st order filter design
#include “SPWS3.h”
#define PI 3.14159265359
#define SAMPLE_RATE 24000.0
#define N 100
#define fl 1000.0
#define fm 2000.0
#define fh 4000.0
// Input samples
float LeftInput;
float RightInput;
// Output samples
float LeftOutput;
float RightOutput;
// Command Gains (GL, GM, and GH) which are changed by push-button
switches 4-6
float GainL = 1.0;
float GainM = 1.0;
float GainH = 1.0;
// Declare any global variables you need
float x[N]={0,0};
float y[N]={0,0};
float z[N]={0,0};
float v[N]={0,0};
int c=0;
int i=0;
float wcl = 2*PI*fl/SAMPLE_RATE;
float wcm = 2*PI*fm/SAMPLE_RATE;
float wch = 2*PI*fh/SAMPLE_RATE;
void Equalizer(void)
{
// TODO: Implement your three band Equalizer containing two
Shelving filter
// and one peaking/notch filter. Use GainL, GainM, and GainH
// as command gains of first, second, and third band respectively.
float B = wch – wcl;
x[c] = LeftInput;
int d=c-1;
int e=c-2;
if (d<0)
{
d=d+N;
}
if (e<0)
{
e=e+N;
}
y[c]=((GainL*tan(wcl/2)+sqrt(GainL))*x[c]+(GainL*tan(wcl/2)
sqrt(GainL))*x[d]-((tan(wcl/2)
sqrt(GainL))*y[d]))/(tan(wcl/2)+sqrt(GainL));
z[c]=((sqrt(GainM)+GainM*tan(B/2))*y[c]
(2*sqrt(GainM)*cos(wcm))*y[d]+(sqrt(GainM)
GainM*tan(B/2))*y[e]+(2*sqrt(GainM)*cos(wcm))*z[d]-(sqrt(GainM)
tan(B/2))*z[e])/(sqrt(GainM)+tan(B/2));
v[c]=((sqrt(GainH)*tan(wch/2)+GainH)*z[c]+(sqrt(GainH)*tan(wch/
2)-GainH)*z[d]-(GainH*tan(wch/2)-1)*v[d])/(GainH*tan(wch/2)+1);
LeftOutput = v[c];
c++;
c = c % N;
// LeftOutput = (GainL)*LeftInput;
}