You are on page 1of 20

Low Pass Butterworth Filter

rp=input('enter the pass band ripple');


rs=input('enter the stop band ripple');
fp=input('enter the pass band frequency');
fs=input('enter the stop band frequency');
f=input('enter the sampling frequency');
wp=(2*fp)/f;
ws=(2*fs)/f;
[n,wn]=BUTTORD(wp,ws,rp,rs);
[b,a]=BUTTER(n,wn);
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
High Pass Butterworth Filter

rp=input('enter the pass band ripple');


rs=input('enter the stop band ripple');
fp=input('enter the pass band frequency');
fs=input('enter the stop band frequency');
f=input('enter the sampling frequency');
wp=(2*fp)/f;
ws=(2*fs)/f;
[n,wn]=BUTTORD(wp,ws,rp,rs);
[b,a]=BUTTER(n,wn,'high');
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');
Band Pass Butterworth Filter

rp=input('enter the pass band ripple');


rs=input('enter the stop band ripple');
wp=input('enter the wp');
ws=input('enter the ws');
[n,wn]=BUTTORD(wp,ws,rp,rs);
[b,a]=BUTTER(n,wn);
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');
Band Stop Butterworth Filter

rp=input('enter the pass band ripple');


rs=input('enter the stop band ripple');
wp=input('enter the wp');
ws=input('enter the ws');
[n,wn]=BUTTORD(wp,ws,rp,rs);
[b,a]=BUTTER(n,wn,'stop');
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');
Chebyshev Type I low pass Filter

rp=input('enter the pass band attenuation in db');


rs=input('enter the stop band attenuation in db');
fp=input('enter the pass band frequency');
fs=input('enter the stop band frequency');
f=input('enter the sampling frequency');
wp=(2*fp)/f;
ws=(2*fs)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rs,wn);
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');
Chebyshev Type II Low pass Filter

rp=input('enter the pass band attenuation in db');


rs=input('enter the stop band attenuation in db');
fp=input('enter the pass band frequency');
fs=input('enter the stop band frequency');
f=input('enter the sampling frequency');
wp=(2*fp)/f;
ws=(2*fs)/f;
[n,wn]=cheb2ord(wp,ws,rp,rs);
[b,a]=cheby2(n,rs,wn);
w=0:.01:pi;
[h,w]=FREQZ(b,a,w,'whole');
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');
Discrete & Continuous Waves

t=0:.30:10;
x=sin(t);
y=cos(t);
subplot(2,2,1),plot(t,x)
title('sine continuous')
xlabel('time')
ylabel('amplitude')
grid
subplot (2,2,2),stem(t,x)
title('sine discrete')
xlabel('time')
ylabel('amplitude')
subplot(2,2,3),plot(t,y)
title('cos continuous')
xlabel('time')
ylabel('amplitude')
grid
subplot (2,2,4),stem(t,y)
title('cos discrete')
xlabel('time')
ylabel('amplitude')
Convolution of two Sequences

a=input('enter first sequence for convolution');


b= input('enter second sequence for convolution');
f=conv(a,b);
subplot(2,2,1),stem(a);
xlabel('time index');
ylabel('amplitude');
title('x[n]')
subplot(2,2,2),stem(b);
xlabel('time index');
ylabel('amplitude');
title('y[n]')
subplot(2,1,2),stem(f);
xlabel('time index');
ylabel('amplitude');
title('convolution of x[n] and y[n]')
Amplitude Modulation

m=input('enter the modulation index');


c=input('enter the carrier amplitude');
f=input('enter the carrier frequency');
q=input('enter message frequency');
t=0:.001:2;
w=c*sin(2*pi*f*t)+((m*c)/2)*cos((2*pi*f*t)-(2*pi*q*t))-
((m*c)/2)*cos((2*pi*f*t)+(2*pi*q*t));
plot(t,w);
xlabel('time');
ylabel('amplitude');
title('AM WAVE');
Chebyshev Type I Band Pass Filter

rp=input('enter the pass band attenuation in db');


rs=input('enter the stop band attenuation in db');
fp1=input('enter the pass band frequency fp1 in Hz');
fp2=input('enter the pass band frequency fp2 in Hz');
fs1=input('enter the stop band frequency fs1 in Hz');
fs2=input('enter the stop band frequency fs2 in Hz');
f=input('enter the sampling frequency');
wp1=(2*fp1)/f;
wp2=(2*fp2)/f;
ws1=(2*fs1)/f;
ws2=(2*fs2)/f;
wp=[wp1,wp2];
ws=[ws1,ws2];
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn);
w=0:.01:pi;
[h,w]=FREQZ(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');

magnitude response
0

-100
gain in db

-200

-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised frequency
phase response
4
phase in radian

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised frequency
ChebyShev Type II Band Stop Filter

rp=input('enter the pass band attenuation in db');


rs=input('enter the stop band attenuation in db');
fp1=input('enter the pass band frequency fp1 in Hz');
fp2=input('enter the pass band frequency fp2 in Hz');
fs1=input('enter the stop band frequency fs1 in Hz');
fs2=input('enter the stop band frequency fs2 in Hz');
f=input('enter the sampling frequency');
wp1=(2*fp1)/f;
wp2=(2*fp2)/f;
ws1=(2*fs1)/f;
ws2=(2*fs2)/f;
wp=[wp1,wp2];
ws=[ws1,ws2];
[n,wn]=cheb2ord(wp,ws,rp,rs);
[b,a]=cheby2(n,rs,wn,'stop');
w=0:.01:pi;
[h,w]=FREQZ(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1),plot(w/pi,m);
grid
xlabel('normalised frequency');
ylabel('gain in db');
title('magnitude response');
subplot(2,1,2), plot(w/pi,an);
grid;
xlabel('normalised frequency');
ylabel('phase in radian');
title('phase response');

magnitude response
50

0
gain in db

-50

-100
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised frequency
phase response
4
phase in radian

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised frequency
DFT and IDFT of a given sequence

i=input('what do you want to perform DFT or IDFT');


X=input('enter the sequence for which the calculation is to be performed');
N=input('enter the range of series i.e. N');
switch lower(i)
case {'dft'}
y=fft(X,N);
case {'idft'}
y=ifft(X,N);
end
mi=abs(X);
ani=angle(X);
m=abs(y);
an=angle(y);
subplot(2,2,1),stem(mi);
title('input sequence magnitude');
xlabel('time index');
ylabel('amplitude');
subplot(2,2,2),stem(ani);
title('input sequence angle');
xlabel('time index');
ylabel('amplitude');
subplot(2,2,3), stem(m);
title('magnitude plot');
xlabel('time index');
ylabel('amplitude');
subplot(2,2,4), stem(an);
title('angle plot');
xlabel('time index');
ylabel('amplitude');

input sequence magnitude input sequence angle


1 1

0.5
amplitude

amplitude

0.5 0

-0.5

0 -1
1 1.5 2 2.5 3 1 1.5 2 2.5 3
time index time index
magnitude plot angle plot
3 2

1
2
amplitude

amplitude

0
1
-1

0 -2
0 2 4 6 8 0 2 4 6 8
time index time index
input sequence magnitude input sequence angle
6 1

0.5
amplitude 4

amplitude
0
2
-0.5

0 -1
0 2 4 6 8 0 2 4 6 8
time index time index
magnitude plot angle plot
1 1

0.5
amplitude

amplitude
0.5 0

-0.5

0 -1
0 2 4 6 8 0 2 4 6 8
time index time index
%program for 20 point moving average filter
t=0:.001:1;
f=1; %frequency of sine wave
y=sin(2*pi*f*t);
%generation of random signal
g=0.5*rand(size(t));
z=g+y;
n=20;%order of the filter
b=1/n.*(ones(1,n));
x=filter(b,1, z);%filter noise
subplot(3,1,1);plot(t,y);ylabe l('pure signal');
subplot(3,1,2);plot(t,z);ylabel('noise burried signal');
subplot(3,1,3);plot(t,x);ylabel('filtered signal');
xlabel('time in seconds');
CIRCULAR CONVOLUTION

function f= circonv(a,b)
a=input('enter the first sequence');
b=input('enter the second sequence');
N1=length(a);
N2=length(b);
N=max(N1,N2);
a=[a zeros(1,N-N1)];
b=[b zeros(1,N-N2)];
for n = 0:N-1
f(n+1)=0;
for i=0:N-1
j=mod(n- i,N);
f(n+1)=f(n+1)+a(i+1)*b(j+1);
end;
end;
subplot(2,2,1);stem(a);xlabel('time inde x');ylabel('amplitude');
subplot(2,2,2);stem(b);xlabel('time index');ylabel('amplitude');
subplot(2,1,2 );stem(f);xlabel('time index');ylabel('amplitude');
title('circular convolution');
FREQUENCY MODULATION
fc=inp ut('enter the carrier frequency in hz');
fm=input('enter the mod ulating frequency in hz');
m=inp ut ('enter the mod ulation inde x');
t=0:.0001:1;
C=sin(2*pi*fc*t);
M=sin(2*p i*fm*t);
Y=sin((2*pi*fc*t)-(m*M));
subplot(3,1,1);plot(t,M);axis([0 1 -1.5 1.5]);
title('modulating signal');xlabel('time');ylabel('amplitude');
subplot(3,1,2);plot(t,C);axis([0 1 -1.5 1.5]);
title('carrier signa l');xlabel('time');ylabel('amplitude');
subplot(3,1,3);plot(t,Y);axis([0 1 -1.5 1.5]);
xlabel('time');ylabel('amplitude');title('FM signal');
FIR FILTER USING VARIOUS WINDOWS
f=input('sampling rate in HZ');
fp= inp ut('enter pass ba nd edge frequency in HZ');
fs=input('enter stop ba nd e dge freque ncy in HZ');
rp=input('passband ripple in dB');
rs=input('enter minimum stop ba nd attenuation in dB');
wp=2*fp/f;
ws=2* fs/f;
[N,wn]=cheb1ord(wp,ws,rp,rs);

%HANN WINDOW

Hw=hann(N+1);
B=fir1(N,wn,Hw);
[H, omega]=freqz(B,1,256);
gain=20*log(abs(H));
subplot(2,2,1);plot(omega/pi,gain);grid;
xlabel('normalised frequency');ylabel('gain(dB)');
title('FIR LPF using HANN window');

%HAMMING WINDOW

Hw=hamming(N+1);
B=fir1(N,wn,Hw);
[H, omega]=freqz(B,1,256);
gain=20*log(abs(H));
subplot(2,2,2);plot(omega/pi,gain);grid;
xlabel('normalised frequency');ylabel('gain(dB)');
title('FIR LPF using HAMMING windo w');

%RECTANGULAR WINDOW

Hw=rectwin(N+1);
B=fir1(N,wn,Hw);
[H, omega]=freqz(B,1,256);
gain=20*log(abs(H));
subplot(2,2,3);plot(omega/pi,gain);grid;
xlabel('normalised frequency');ylabel('gain(dB)');
title('FIR LPF using RECTANGULAR window');

%TRIANGULAR WINDOW

Hw=triang(N+1);
B=fir1(N,wn,Hw);
[H, omega]=freqz(B,1,256);
gain=20*log(abs(H));
subplot(2,2,4);plot(omega/pi,gain);grid;
xlabel('normalised frequency');ylabel('gain(dB)');
title('FIR LPF using TRIANGULAR window');
AUTOCORRELATION AND CROSSCORRELATION

x=input('enter the first sequence');


I=input('whether u want autocorrelation(press 1) or cross corelation(press 2)..??');
if I == 1
Z=xcorr(x);
subplot(2,1,1);stem(x); xlabel('t ime');
ylabel('magnitude');title('input sequence');
subplot(2,1,2);stem(z); xlabel('time');
ylabel('magnitude');title('autocorrelation');
elseif I == 2
y=input('enter the second sequence');Z=XCORR(x,y);
subplot(3,1,1);stem(x); xlabel('t ime');
ylabel('magnitude');title('input sequence "x"');
subplot(3,1,2);stem('y'); xlabel('t ime');
ylabel('magnitude');title('input sequence "y"');
subplot(3,1,3);stem(z); xlabel('time');
ylabel('magnitude');title('crosscorelation');
else
disp invalidinput;
end;
enter the first sequence [2 3 2 -4 5 6 -4 -6 3 2 1 5]
whether u want autocorrelation(press 1) or cross correlation(press 2)..??1
enter the first sequence[1 2 3 -4 5 -6 8 4 -3 -4]
whether u want autocorrelation(press 1) or cross corelation(press 2)..??2
enter the second sequence[5 4 3 -6 -9 0 1 3 -1 -3 -4]

You might also like