Sunteți pe pagina 1din 79

SIGNAL PROCESSING

%program to find inverse of matrix


clc;clear all;close all;
n=input('enter the n value');
m=input('enter the m value');
if(n~=m)
display('not a square matrix,inverse cant be found');
else
for i=1:n,
for j=1:n,
A(i,j)=input(' enter the elements one by one');
end
end
display('Given matrix A is');
A
if det(A)==0
display('cannot find inverse as determinent is zero');
else
for i=1:n,
for j=1:n,
B=A;
B(:,j)=[];
B(i,:)=[];
C=B;
D(i,j)=((-1)^(i+j))*det(C);
end
end
display('Adjoint of matrix A is');
D
display('Inverse of matrix A is');
Inverse=D'/det(A)
end
end
OUTPUT
enter the n value3
enter the m value3
enter the elements one by one5
enter the elements one by one6
enter the elements one by one3
enter the elements one by one9
enter the elements one by one6

enter the elements one by one7


enter the elements one by one5
enter the elements one by one6
enter the elements one by one2
Given matrix A is
A=
5
9
5

6
6
6

3
7
2

Adjoint of matrix A is
D=
-30 17 24
6 -5 0
24 -8 -24
Inverse of matrix A is
Inverse =
-1.2500 0.2500 1.0000
0.7083 -0.2083 -0.3333
1.0000
0 -1.0000
WAVE FORMS
PROGRAM 1
x = 0:0.01:pi;
y = sin(x);
plot(x,y)
t=0:0.1:5;
y=cos(t)
plot(t,y)
OUTPUT

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.5

1.5

Program 2:
%TO Generate a UNIT step Sequence
N=21;
x=ones(1,N);
n=0:1:N-1;
subplot(2,2,1);
stem(n,x);
xlabel('n')
ylabel('x(n)');
title('UNIT STEP SEQUENCE');
OUTPUT

2.5

3.5

4.5

UNIT STEP SEQUENCE

x(n)

0.5

10
n

15

20

%Addition of two sinusoidal sequences


x1=sin(.1*pi*n)+sin(.2*pi*n);
subplot(2,2,2);
stem(n,x1);
xlabel('n')
ylabel('x1(n)');
title('Aiddition of two sin sequences');
OUTPUT

Aiddition of two sin sequences

x1(n)

1
0
-1
-2

%Exponential sequence
x2=.8.^(n);
subplot(2,2,3)
stem(n,x2);
xlabel('n');
ylabel('x2(n)');
title('Exponential sequences');
OUTPUT

10
n

15

20

Exponential sequences

x2(n)

0.5

10
n

% SInusoidal sequence
x3=sin(.1*pi*n);
subplot(2,2,4)
stem(n,x3);
xlabel('n');
ylabel('x3(n)');
xlabel('n');
ylabel('x3(n)');
title('Sinusoidal sequences');
OUTPUT

15

20

Sinusoidal sequences

x3(n)

0.5
0
-0.5
-1

% Con volution OF two sequences


clc;
clear all;
close all;
x=[1,0,-1,1,2,1];
N1=length(x);
h=[1,1,1,1,1]
N2=length(h);
y=conv(x,h)
n=0:1:N1+N2-2;
stem(n,y);
title('Convolution of Two sequences');
xlabel('n');
ylabel('y(n)');
OUTPUT

10
n

15

20

Convolution of Two sequences

4
3.5
3

y(n)

2.5
2
1.5
1
0.5
0

COS WAVE FORM


clc;clear all;close all;
t=0:0.1:5;
y=cos(t*pi/2);
subplot(2,2,4);
plot(t,y)
ylabel('----AMPLITUDExlabel('-----t');
title('COS WAVE FORM ');
OUTPUT

COS WAVE FORM

------->Amplitude

1
0.5
0
-0.5
-1

%SIN WAVEFORM
t=-5:0.1:5;
y=sin(t*pi/2);
subplot(2,2,2);
plot(t,y)
xlabel('-----t');
ylabel('----AMPLITUDE');
title('SIN WAVE FORM ');
OUTPUT

------->t

----AMPLITUDE

1
0.5
0
-0.5
-1
-5

%TAN WAVEFORM
t=0:0.1:5;
y=tan(t*pi/2);
subplot(2,2,3);
plot(t,y)
xlabel('-----t');
ylabel('----AMPLITUDE');
title('tan WAVE FORM ');
OUTPUT

SIN WAVE FORM

0
-----t

16

----AMPLITUDE

x 10

tan WAVE FORM

1
0
-1

-----t

%COT WAVEFORM
t=0:0.1:5;
y=cot(t*pi/2);
subplot(2,2,4);
plot(t,y)
xlabel('-----t');
ylabel('----AMPLITUDE');
title('COT WAVE FORM ');
OUTPUT

15

----AMPLITUDE

x 10

cot WAVE FORM

0
-5
-10

-----t

% TRiangular Pulse Wave Form


clc;
clear all;
close all;
t=-20:0.10:20;
y=tripuls(t,25);
%Subplot(2,1,1);
plot(t,y,'k');
xlabel('------->t');
ylabel('------->Amplitude')
title('Triangular Pulse');
OUTPUT

Triangular Pulse

1
0.9
0.8

------->Amplitude

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-20

-15

-10

-5

%swept Frequency Cosine Waveform


n=0:100;
a=pi/2/100;
b=0;
arg=a*n.*n+b*n;
x=cos(arg);
subplot(2,1,1);
plot(n,x,'k');
axis([0 50 -1.5 1.5]);
xlabel('------>t')
ylabel('------>amplitude');
title('SWept Frequency cosine');
OUTPUT

0
------->t

10

15

20

------>amplitude

SWept Frequency cosine


1
0
-1
0

10

15

20

25
------>t

30

35

% To Plot the Frequency Response of the First order System


clear all;
b=[1];
a=[1,-.8];
w=0:.01:2*pi;
[h]=freqz(b,a,w);
subplot(2,1,1),plot(w/pi,abs(h));
title('frquency response of first order systemh(n)=0.^nu(n)');
xlabel('Normalised frequency')
ylabel('magnitude');
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('normalised frequency');
ylabel('phase in Radians');
OUTPUT

40

45

50

frquency response of first order systemh(n)=0. nu(n)

magnitude

6
4
2
0

0.2

0.4

0.6

0.8
1
1.2
Normalised frequency

1.4

1.6

1.8

0.2

0.4

0.6

0.8
1
1.2
normalised frequency

1.4

1.6

1.8

phase in Radians

1
0.5
0
-0.5
-1

%To plot the frequency response of the system


clear all;
b=[1,0,.9];
a=[1,0,.4];
d=[1,-1];
f=[1,0,.4];
[h,ph]=freqz(b,a);
[h1,ph1]=freqz(d,f);
subplot(2,2,1);
plot(ph/pi,abs(h));
grid;
xlabel('Normalised frequency')
ylabel('magnitude');
subplot(2,2,2);
plot(ph1/pi,abs(h1));
grid;
xlabel('Normalised frequency')
ylabel('Magnitude');
subplot(2,2,3);
plot(ph/pi,angle(h));
grid;
xlabel('Normalised frequency')
ylabel('phase in radians');

subplot(2,2,4);
plot(ph1/pi,angle(h1));
grid;
xlabel('Normalised frequency')
ylabel('phase in Radians');

1.5

Magnitude

magnitude

OUTPUT

0.5
0

0.5
Normalised frequency

0.5
0
-0.5
0

0.5
Normalised frequency

% TRIANGULAR PULSE WAVE FORM


t=linspace(-5*pi,5*pi);
y=tripuls(t,10);
Subplot(2,1,1);
plot(t,y,'k');
xlabel('------->t');
ylabel('------->Amplitude')
title('Triangular Pulse');
OUTPUT

0.5
Normalised frequency

0.5
Normalised frequency

2
phase in Radians

phase in radians

-1

1
0
-1

------->Amplitude

0.5

0
-20

% RECTANGULAR PULSE WAVEFORM


t=linspace(-5*pi,5*pi);
y=rectpuls(t,10);
subplot(3,2,1);
plot(t,y,'k');
xlabel('------>t');
ylabel('----->Amplitude');
title('Rectangulart Pulse');
OUTPUT

Triangular Pulse

-10

0
------->t

10

20

----->Amplitude

Rectangular Pulse

0.5
0
-20

-10

0
------>t

10

20

3) % PERIODIC SINC WAVE FORM


x=-pi:0.05:pi;
y=sinc(x);
subplot(2,1,1);
plot(x,y);
xlabel('------>t');
ylabel('----->Amplitude');
title('Periodic Sinc');
OUTPUT

Periodic Sinc

----->Amplitude

1
0.5
0
-0.5
-4

-3

-2

-1

0
------>t

%SWEPT FREQUENCY COSINE WAVEFORM


n=0:100;
a=pi/2/100;
b=0;
arg=a*n.*n+b*n;
x=cos(arg);
subplot(2,1,1);
plot(n,x,'k');
axis([0 50 -1.5 1.5]);
xlabel('------>t')
ylabel('------>amplitude');
title('SWept Frequency cosine');
OUTPUT:

------>amplitude

SWept Frequency cosine


1
0
-1
0

10

%SQUARE WAVE FORM


z=linspace(-3*pi,3*pi);
y=Square(z);
subplot(2,1,2);
plot(z,y,'k');
xlabel('------>t')
ylabel('------>amplitude');
title('Square wave form');
OUTPUT

15

20

25
------>t

30

35

40

45

50

Square wave form

------>amplitude

1
0.5
0
-0.5
-1
-10

-8

-6

-4

-2

0
------>t

PROGRAM TO GENERATE A SAW TOOTH WAVEFORM


clc;
clear al;
close all;
t=(-5*pi:0.05:5*pi);
y=sawtooth(t);
subplot(2,1,1);
plot(y)
OUTPUT:

10

1
0.5
0
-0.5
-1

100

200

300

400

%UNIT STEP SEQUENCE


clc;
clear all;
N=input('Enter the length of unit step sequence:');
t=0:1:N-1;
y=ones(1,N);
subplot(2,1,1);
stem(t,y,'k','filled');
axis([0 N 0 2]);
xlabel('----------->t');
ylabel('----------->amplitude');
title(' Unit step sequence');
OUTPUT:

500

600

700

Unit step sequence

----------->amplitude

2
1.5
1
0.5
0

3
----------->t

%DESCREATE TIME SIGNAL


fs=input('Enter the Sampling Rate')
ts=1/fs;
n=25:25;
nts=n*ts;
dt= 0.00005;
t=-.005:dt:.005;
xa=exp(-1000*abs(t));
xn=exp(-1000*abs(nts));
subplot(3,1,1);
plot(t*1000,xa);
xlabel('----->t in msec');
ylabel('x(t)');
title('Continuous Time Signalx(t)');
subplot(3,1,2);
stem(xn);
xlabel('----->n');
ylabel('x(n)');
title('discreate Time SIgnal x(t)');
title('ts=0.1 msec');
%reconstruction of the signal from its sample
xa=xn*sin(fs*ones(length(n),1)*t-nts*ones(1,length(t)));

subplot(3,1,3);
plot(t,xa);
xlabel('----->t in msec');
ylabel('x(t)');
title('Reconstructed signalx(t)fromx(n)');
OUTPUT:

Enter the Sampling Rate8


fs =
8
Continuous Time Signalx(t)

x(t)

1
0.5
0
-5

-4

-3

-2

-1
0
1
----->t in msec
ts=0.1 msec

0.2

0.4

0.6

0.8

x(n)

1.4

1.6

1.8

0
-1

1
----->n

1.2

%FINDING REAL AND IMAGINARY PARTS OF COMPLEX NUMBER


%inputs: comp_number------complex number
%outputs re and img-----real and imaginary numbers respectively
function[Re,img]=reimg(comp_number);
comp_number=3+4i;
N=length(comp_number);
for i=1:N
re(i)=abs(comp_number(i))*cos(angle(comp_number(i)));
img(i)=abs(comp_number(i))*sin(angle(comp_number(i)));

end
subplot(2,2,1)
stem(1:1:N,comp_number);
title('input signal');
xlabel('time');
ylabel('amplitude')
subplot(2,2,2)
stem(1:1:length(re),re);
title('real part of signal')
xlabel('time');
ylabel('amplitude')
subplot(2,2,3)
stem(1:1:length(img),img)
title('imaginary part of signal')
xlabel('time');
ylabel('amplitude')
OUTPUT:
input signal

2
1
0

amplitude

real part of signal

4
amplitude

amplitude

0.5

1
1.5
time
imaginary part of signal

3
2
1
0

0.5

1
time

1.5

3
2
1
0

0.5

1
time

1.5

FUNCTION TO CALCULATE CONVOLUTION OF TWO SEQUENCES.


% INPUTS: x1 & x2 are two input sequences
%OUTPUT: Y CONVOLUTION OF x1 & x2.
%usage:[a]=convolve(x1,x2) at mat lab prompt.

function y=convolve(x1,x2)

x1=5;
x2=6;
m=length(x1);
n=length(x2);
k=m+n-1;
x1=[x1 zeros(1,n-1)];
x2=[x2 zeros(1,m-1)];
y=zeros(1,k);
for i=1:k
for j=1:i
if i==1
y(i)=x1(1)*x2(1);
else
y(i)=y(i)+x1(i-j+1)*x2(j);
end
end
end
subplot(2,2,1)
stem(1:1:m,x1)
title('first Input signal')
xlabel('time')
ylabel('Amplitude')
subplot(2,2,2)
stem(1:1:n,x2)
title('Second Input signal')
xlabel('time')
ylabel('Amplitude')
subplot(2,2,3)
stem(1:1:k,y)
title('Convolved signal')
xlabel('time')
ylabel('Amplitude')
OUTPUT:

% length of first input sequence.


%length of second input sequence.
% length of convolved sequence.
%append zeros for ease of computation.
% append zeros for ease of computation.
% initialisation of response of thwe system.

first Input signal

4
2
0

0.5

0.5

Amplitude

30

1
1.5
time
Convolved signal

Second Input signal

6
Amplitude

Amplitude

4
2
0

0.5

1
time

1.5

20
10
0

1
time

1.5

% FUNCTION TO CALUCLATE THE OUTPUT A SEQUENCE USING


HAMMING WINDOW
%INPUTS: rp-----pass band ripple
%
fp-----pass band frequency
%
fs-----stop band frequncy
%
f-----Sampling frequency
%outputs: Magnitude and phase response of LPF,HPF,BPF
function x=hamm(rp,rs,fp,fs,f)
rp=0.02;
rs=0.01;
fp=1200;
fs=1700;
f=9000;
wp=2*fp/f;
% Width of pass band Frequency
ws=2*fs/f;
% Width of Stop band Frequency
num=-20*log10(sqrt(rp*rs))-13;
%caluclate the Number of Numerator Coeff
dem=14.6*(fs-fp)/f;
% caluclate the number denominator Co efficients
n=ceil(num/dem);
%no of window terms considered
if rem(n,2)~=0
n=n-1;
end
for i=1:n+1

wn(i)=0.54-.46*cos(2*pi*i/(n-1));
end
% LOW PASS FILTER
b=fir1(n,wp,wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
title('Lowpass filter')
ylabel('gain in db')
xlabel('(a)Normalized Frequency ---->');
%HIGH PASS FILTER
b=fir1(n,wp,'high',wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m);
title('High pass Filter')
ylabel('gain in db')
xlabel('(a)Normalized Frequency ---->');
%BAND PASS FILTER
bw=[wp ws];
b=fir1(n,bw,wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m);
title('band pass filter')
ylabel('gain in db')
xlabel('(a)Normalized Frequency ---->');
%BAND STOP FILTER
b=fir1(n,wp,'stop',wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4)
plot(o/pi,m);
title('band stop Filter')
ylabel('gain in db')
xlabel('(a)Normalized Frequency ---->');

OUTPUT OF THE HAMMING WINDOW

50

Lowpass filter

gain in db

gain in db

0
-50
-100

-20
-40
-60
-80

0
0.5
1
(a)Normalized Frequency ---->

High pass Filter

0
-20
-40
-60

0
0.5
1
(a)Normalized Frequency ---->
band pass filter

50
gain in db

gain in db

20

0
0.5
1
(a)Normalized Frequency ---->
band stop Filter

0
-50
-100

0
0.5
1
(a)Normalized Frequency ---->

%FUNCTION TO CALCULATE THE IMPULSE RESPONSE OF BLACKMAN


(HANNING) WINDOW
%inputs : rp--pass band ripple
%
fp---stop band frequency
%
fs---stop band frequency
%
f--- sampling freqency
% outputs : magnitude and phase response of lpf,hpf, bpf
function x=hann(rp,rs,fp,fs,f)
rp=.03;
rs=.01;
fs=2000;
fp=1400;
f=8000;
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);

n1=n+1;
if rem(n,2)~=0
n=n-1;
end
%BLACKMAN WINDOW CO-EFFICIENTS
for i=1:n+1
wn(i)=0.54-.46*cos(2*pi*i/(n-1));
end
%LOW PASS FILTER
bw=[wp ws];
b=fir1(n,bw,wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m)
ylabel('gain in db')
xlabel('(a)normalized freqency----->');
b=fir1(n,bw,'stop',wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m)
ylabel('gain in db')
xlabel('(a)normalized freqency----->');
title('bsf')
b=fir1(n,wp,wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m)
ylabel('gain in db')
xlabel('(a)normalized freqency----->');
b=fir1(n,wp,'high',wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m)
ylabel('gain in db')
xlabel('(a)normalized freqency----->');

OUTPUT OF THE HANNING WINDOW

gain in db

gain in db

20

-20
-40
-60

-5
-10
0

0.5
(a)normalized freqency----->

0.5
(a)normalized freqency----->

0.5
(a)normalized freqency----->

20

-15

-40
-60

gain in db

gain in db

0.5
(a)normalized freqency----->
bsf

-20

%INOUT PLOTS
income = [3.2 4.1 5.0 5.6];
outgo = [2.5 4.0 3.35 4.9];
subplot(2,1,1); plot(income)
subplot(2,1,2); plot(outgo)

0
-20
-40
-60

6
5
4
3

1.5

2.5

3.5

1.5

2.5

3.5

5
4.5
4
3.5
3
2.5

%CALCULATION OF POWER SPECTRAL DENSITY (PSD)


function sx=psd(x);
x=100;
n=length(x);
u=2*n-1;
x=[x zeros(1,n-1)];
rx=zeros(1,1);

%length of the input sequence


%insert %extra zeros to compensate extra length
% initialize auto corellation

%CALUCLATE THE AUTO CORRELATION OF A SEQUENCE


for i=1:u
for j=1:i
if i==1
rx(i)=rx(i)+x(i-j+1)*x(j);
end
end
end

%COMPUTE PSD
sx=fft(rx,1024);
% PLOTS
subplot(2,2,1);
plot(x);
title('inputsignal');
xlabel('Time');
ylabel('Amplitude');
subplot(2,2,2);
plot(10*log10(abs(sx)));
title('magnitude spectrum');
xlabel('Frequency');
ylabel('power in db');
subplot(2,2,3);
plot(angle(sx));
title('phase spectrum');
xlabel('Frequency');
ylabel('phase');

%original input signal

%Magnitude spectrum of psd

%phase spectrum of psd

OUT PUT OF POWER SPECTRAL DENSITY


inputsignal

100.5
100
99.5
99

0.5

1
1.5
Time
phase spectrum

500
1000
Frequency

1500

phase

0.5
0
-0.5
-1

magnitude spectrum

41
power in db

Amplitude

101

40.5
40
39.5
39

500
1000
Frequency

1500

% IMPULSE RESPONSE OF THE SYSTEM


function y=outsys(x,h)
x=2;
h=10;
m=length(x);% Length of excitation
n=length(h);% Length of impulse response of system
k=m+n-1;%length of response
x=[x zeros(1,n-1)];% Append zeros for ease of computation
h=[h zeros(1,m-1)];% Append zeros for ease of computation
y=zeros(1,k);% Initialisation of response of the system
%compute the output of the system
for j=1:i
if i==1
y(i)=x(1)*h(1);
end
end
subplot(2,2,1)
stem(1:1:m,x);
xlabel('time');
ylabel('ampltue');
subplot(2,2,2);% impulse response of the system
stem(n,h);
title('impulse response of system');
xlabel('time');
ylabel('ampltude');
subplot(2,2,3);%output of the system
stem(k,y);
title('response of system');
xlabel('time');
ylabel('ampltude');
OUTPUT:

ampltude

ampltue

1.5
1
0.5
0

1
1.5
time
response of system

0.5

ampltude

impulse response of system

10

0.5

0.5

1
time

1.5

0.5
0
-0.5
-1

1
time

1.5

%FUNCTION TO CALCULATE THE DFT OF A GIVEN SEQUENCE


%INPUTS: x---Input sequence
%OUTPUT: X---DFT of a given sequence
%**********************************************************
function[X]=DFT2(x)
%***********************************************************
x=5;
N=length(x);%length of input sequence
for k=0:N-1
for n=0:N-1
wn(k+1,n+1)=exp((j*2*pi/N)*k*n);
end
end
%DFT of a given sequence
X=wn*x';
subplot(2,2,1)
stem(1:1:N,x)
title('Input Signal')
xlabel('Time')
ylabel('Amplitude')

subplot(2,2,2)
stem(10*log10(abs(X)))
title('MAGNITUDE RESPONSE OF PSD')
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,2,3)
stem(angle(X))
title('PHASE RESPONSE OF PSD')
xlabel('Frequency')
ylabel('Phase')
OUTPUT
Input Signal

MAGNITUDE RESPONSE OF PSD


8

Amplitude

Amplitude

2
0

0.5

1
1.5
2
Time
PHASE RESPONSE OF PSD

Phase

0.5
0
-0.5
-1

0.5

1
1.5
Frequency

%BLACKMAN WINDOW
function x=blackman1(rp,rs,fp,fs,f)
rp=0.03;
rs=0.01;
fp=2000;

6
4
2
0

0.5

1
1.5
Frequency

fs=2500;
f=7000;
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if rem(n,2)~=0
n1=n;
n=n-1;
end
for i=1:n+1
wn(i)=0.42-0.5*cos(2*pi*i/(n-1))+0.08*cos(4*pi*i/(n-1));
end
%low pass filter
b=fir1(n,wp,wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1)
plot(o/pi,m)
title('low pass filter')
ylabel('gain in db')
xlabel('(a) Normalised Frequency------>');
%HPF
b=fir1(n,wp,'high',wn);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2)
plot(o/pi,m);
title('high pass filter')
ylabel('gain in db')
xlabel('(a) Normalized Frequency------->');
OUTPUT OF THE BLACK MAN WINDOW

low pass filter

0
-20
-40
-60

g a in in d b

g a in in d b

20

0
0.5
1
(a) Normalised Frequency------>

high pass filter

-20
-40
-60
-80

0
0.5
1
(a) Normalized Frequency------->

RESPONSE OF A DIGITAL FILTER(FIR FILTER)


% Frequency response of a digital filter
%y(n)=x(n)-x(n-1)
%Filter Definition
b=[1 -1];
a=[1 0];
% Frequency response implementation
[h,th]=freqz(b,a,32);
% Frequency response plot
clf
subplot(2,2,1);
plot(th,abs(h));
title('maginitude Response');
subplot(2,2,2);
plot(th,angle(h));
title('phase Response');
xlabel('Radians');
% IMPULSE RESPONSE CALCLATION AND PLOT
x=[1 zeros(1,10)];
y=filter(b,a,x);
subplot(2,2,3);
stem(y);
xlabel('seconds');
title('impulse response');

%POLE-ZERO PLOT
[z,p]=tf2zp(b,a);
subplot(2,2,4);
zplane(z,p);

OUTPUT OF THE FIR FILTER RESPONSE


maginitude Response

2
1.5

1.5

0.5

0.5

impulse response

2
Radians

1
Imaginary Part

0.5
0
-0.5
-1

phase Response

seconds

10

15

0.5
0
-0.5
-1
-1

RESPONSE OF A DIGITAL FILTER(IIR FILTER)


% Frequency response of a digital filter
%y(n)=x(n)-x(n-1)
%Filter Definition

-0.5
0 0.5
Real Part

b=[.0013 .0064 .0128 .0128 .0064 .0013];


a=[1.0 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
% Frequency response implimentation
[h,th]=freqz(b,a,128);
% Frequency response plot
clf
subplot(2,2,1);
plot(th,abs(h));
title('maginitude Response');
subplot(2,2,2);
plot(th,angle(h));
title('phase Response');
xlabel('Radians');
% IMPULSE RESPONSE CALCLATION AND PLOT
x=[1 zeros(1,20)];
y=filter(b,a,x);
subplot(2,2,3);
stem(y);
xlabel('seconds');
title('impulse response');
%pole-zero plot
[z,p,k]=tf2zp(b,a);
subplot(2,2,4);
zplane(z,p)
OUTPUT OF THE IIR FILTER RESPONSE

maginitude Response

1.5

phase Response

4
2

0
0.5
0

-2
0

impulse response

0.3

2
Radians

1
Imaginary Part

0.2
0.1
0
-0.1

-4

10
20
seconds

30

0.5
0
-0.5
-1
-1

0
Real Part

% ksdensity -Compute density estimate using kernel-smoothing method


x = [randn(30,1); 5+randn(30,1)];
[f,xi] = ksdensity(x);
plot(xi,f);
OUTPUT:

0.12

0.1

0.08

0.06

0.04

0.02

0
-8

-6

-4

-2

%CHEBYCHEV TYPE-1 LOW PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
[b,a]=cheby1(n,rp,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
title('magnitude of the system');
subplot(2,1,2);
plot(om/pi,an);

10

12

ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
title('phase of the system');
OUT PUT OF THE %CHEBYCHEV TYPE-1 LOW PASS FILTER
Enter the pass band ripple0. 2
Enter the stop band ripple45
Enter the pass band freq1300
Enter the stop band freq1500
Enter the Sampling Frequency10000
magnitude of the system

Gain in db--->

0
-100
-200
-300
-400

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->
phase of the system

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 HIGH PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');

w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
[b,a]=cheby1(n,rp,wn,high,'s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
title('magnitude of the system');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
title('phase of the system');
OUTPUT OF THE CHEBYCHEV TYPE-1 HIGH PASS FILTER
Enter the pass band ripple0.3
Enter the stop band ripple60
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency9000

magnitude of the system

Gain in db--->

0
-50
-100
-150
-200

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->
phase of the system

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 BAND PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,bandpass','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);

plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
CHEBYCHEV TYPE-1 BAND PASS FILTER
Enter the pass band ripple0.4
Enter the stop band ripple35
Enter the pass band freq2000
Enter the stop band freq2500
Enter the Sampling Frequency10000

Gain in db--->

0
-200
-400
-600

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-1 BAND STOP FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;

w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,stop','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
CHEBYCHEV TYPE-1 BAND STOP FILTER
Enter the pass band ripple0.25
Enter the stop band ripple40
Enter the pass band freq2500
Enter the stop band freq2750
Enter the Sampling Frequency7000

Gain in db--->

50

-50

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

%CHEBYCHEV TYPE-2 LOW PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
[b,a]=cheby2(n,rp,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');

OUTPUT OF THE CHEBYCHEV TYPE-2 LOW PASS FILTER


Enter the pass band ripple.0.35
Enter the stop band ripple35
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

0
-20
-40
-60

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE-2 HIGH PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
[b,a]=cheby2(n,rp,wn,'high','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);

ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
CHEBYCHEV TYPE-2 HIGH PASS FILTER
Enter the pass band ripple0.25
Enter the stop band ripple40
Enter the pass band freq1400
Enter the stop band freq1800
Enter the Sampling Frequency7000

Gain in db--->

10
0
-10
-20
-30

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE-2 BAND PASS FILTER


clc;
clear all;

format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb2ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby2(n,rp,wn,'high','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
CHEBYCHEV TYPE-2 BAND PASS FILTER
Enter the pass band ripple0.4
Enter the stop band ripple40
Enter the pass band freq1400
Enter the stop band freq2000
Enter the Sampling Frequency9000

Gain in db--->

50
0
-50
-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

%CHEBYCHEV TYPE2 BAND STOP FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2];
[b,a]=cheby1(n,rp,wn,stop','s');
w=0:.01/pi:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');

xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
CHEBYCHEV TYPE-2 BAND STOP FILTER
Enter the pass band ripple0.3
Enter the stop band ripple46
Enter the pass band freq1400
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

5
0
-5
-10
-15

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

% BUTTER WORTH LOW PASS FILTER


clc;
clear all;
format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');

fs=input('Enter the Sampling Frequency');


w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
%[b,a]=zp2tf(z,p,k);
[b,a]=butter(n,wn,'s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
OUTPUT OF THE BUTTERWORTH LOW PASS FILTER
Enter the pass band ripple.6
Enter the stop band ripple.9
Enter the pass band freq1200
Enter the stop band freq1800
Enter the Sampling Frequency4000

Gain in db--->

0
-2
-4
-6

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

0
-0.5
-1
-1.5

% BUTTER WORTH HIGH PASS FILTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
%[z,p,k] = butter(n,Wn,'s')
%[b,a] = butter(n,Wn)
%[b,a]=butter(n,wn,'s');
[b,a] = butter(9,300/500,'high')
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);

ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
OUTPUT OF THE BUTTERWORTH HIGH PASS FILTER
Enter the pass band ripple.6
Enter the stop band ripple.9
Enter the pass band freq1200
Enter the stop band freq1800
Enter the Sampling Frequency4000
b=
Columns 1 through 7
0.0011 -0.0096

0.0384 -0.0895

0.1342 -0.1342

0.0895

1.3708

0.1993

Columns 8 through 10
-0.0384

0.0096 -0.0011

a=
Columns 1 through 7
1.0000

1.7916

2.5319

Columns 8 through 10
0.0431

0.0058

0.0004

2.1182

0.6090

Gain in db--->

20
0
-20
-40
-60

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

4
2
0
-2
-4

% BUTTER WORTH BAND PASS FILTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
wp=input('Enter the pass band freq');
ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs,'s');
wn=[w1,w2];
%[z,p,k] = butter(n,Wn,'s')
%[b,a] = butter(n,Wn)
%[b,a]=butter(n,wn,'s');
[b,a] = butter(n,wn,'bandpass','s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);

plot(om/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
subplot(2,1,2);
plot(om/pi,an);
ylabel('Phase in radians-->');
xlabel('(b) Normalised Frequency--->');
OUTPUT OF THE BUTTERWORTH BAND PASS FLTER
Enter the pass band ripple0.6
Enter the stop band ripple9
Enter the pass band freq100
Enter the stop band freq800
Enter the Sampling Frequency4000

Gain in db--->

0
-5
-10
-15
-20

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(a)Normalised Frequency--->

0.8

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
(b) Normalised Frequency--->

0.8

0.9

Phase in radians-->

2
1
0
-1
-2

% BARLETT LOW PASS FLTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');

rs=input('Enter the stop band ripple');


fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=bartlett(n1);
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
ylabel('Gain in db--->');
xlabel('(a)Normalised Frequency--->');
OUTPUT OF THE BARLETT LOW PASS FLTER
Enter the pass band ripple0.04
Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

0
-10
-20
-30
-40

0.5
1
(a)Normalised Frequency--->

% BARLETT HIGH PASS FLTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=bartlett(n1);
b=fir1(n,wp,high,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));

subplot(2,2,2);
plot(o/pi,m);
ylabel('Gain in db--->');
xlabel('(b)Normalised Frequency--->');
OUTPUT OF THE BARLETT HIGH PASS FLTER
Enter the pass band ripple0.04
Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

10
0
-10
-20
-30

% BARLETT BAND PASS FLTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;

0.5
1
(b)Normalised Frequency--->

dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=bartlett(n1);
wn=[wp ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m);
ylabel('Gain in db--->');
xlabel('(c)Normalised Frequency--->');
OUTPUT OF THE BARLETT BAND PASS FLTER
Enter the pass band ripple0.04
Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

10
0
-10
-20
-30
-40

0.5
1
(c)Normalised Frequency--->

% BARLETT BAND STOP FLTER


clc;
clear all;
%format long
rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple');
fp=input('Enter the pass band freq');
fs=input('Enter the stop band freq');
f=input('Enter the Sampling Frequency');
wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=bartlett(n1);
wn=[wp ws];
b=fir1(n,wn,stop,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m);
ylabel('Gain in db--->');
xlabel('(d)Normalised Frequency--->');
OUTPUT OF THE BARLETT BAND STOP FLTER
Enter the pass band ripple0.04
Enter the stop band ripple0.02
Enter the pass band freq1500
Enter the stop band freq2000
Enter the Sampling Frequency8000

Gain in db--->

2
0
-2
-4
-6
-8

0.5
1
(d)Normalised Frequency--->

%fir1-Window-based finite impulse response filter design


b = fir1(48,[0.35 0.65]);
freqz(b,1,512);
OUTPUT:

Magnitude (dB)

50
0
-50
-100

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000

%fir1-Window-based finite impulse response filter design


load chirp
% Load y and fs.
b = fir1(34,0.48,'high',chebwin(35,30));
freqz(b,1,512)
OUTPUT:

Magnitude (dB)

50
0
-50
-100
-150

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

0.1

0.2

0.3
0.4
0.5
0.6
0.7
0.8

Normalized Frequency ( rad/sample)

0.9

Phase (degrees)

1000
0
-1000
-2000

%Filter a signal using a filter with various initial conditions (IC) or no initial conditions.
x = randn(100,1);
% Original signal.
b = fir1(50,.4);
% 50th-order linear-phase FIR filter.
hd = dfilt.dffir(b); % Direct-form FIR implementation.
% Do not set specific initial conditions.
y1 = filter(hd,x);
zf = hd.states;

% 'PersistentMemory' is 'false' (default).


% Final conditions.

%Now use nonzero initial conditions by setting ICs after before you filter.
hd.persistentmemory = true;
hd.states = 1;
% Uses scalar expansion.
y2 = filter(hd,x);
stem([y1 y2])
% Different sequences at the beginning.
OUTPUT:

1.5

0.5

-0.5

-1

-1.5

10

20

30

40

50

60

70

80

90

100

Communication
% Sample the signal 100 times per second, for 2 seconds.
%ammod-Amplitude modulation.
%ssbmod-Single sideband amplitude modulation.
%fft-Discrete Fourier transform.
%abs-Absolute value and complex magnitude.
Fs = 100;
t = [0:2*Fs+1]'/Fs;
Fc = 10; % Carrier frequency
x = sin(2*pi*t); % Sinusoidal signal
% Modulate x using single- and double-sideband AM.
ydouble = ammod(x,Fc,Fs);
ysingle = ssbmod(x,Fc,Fs);
% Compute spectra of both modulated signals.
zdouble = fft(ydouble);
zdouble = abs(zdouble(1:length(zdouble)/2+1));
frqdouble = [0:length(zdouble)-1]*Fs/length(zdouble)/2;
zsingle = fft(ysingle);
zsingle = abs(zsingle(1:length(zsingle)/2+1));
frqsingle = [0:length(zsingle)-1]*Fs/length(zsingle)/2;
% Plot spectra of both modulated signals.
figure;
subplot(2,1,1); plot(frqdouble,zdouble);
title('Spectrum of double-sideband signal');
subplot(2,1,2); plot(frqsingle,zsingle);
title('Spectrum of single-sideband signal');
OUTPUT:

Spectrum of double-sideband signal

60
40
20
0

10

15

20

25

30

35

40

45

50

40

45

50

Spectrum of single-sideband signal

100

50

10

15

20

25

30

35

AM Demodulation
%butter- Design Butterworth IIR digital filter using the specifications in filter
specification object.
%ammod- Amplitude modulation
%amdemod- Amplitude demodulation
t = .01;
Fc = 10000; Fs = 80000;
t = [0:1/Fs:0.01]';
s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal
figure(1) ;plot(s) ;
title(Modulating signal) ;
[num,den] = butter(10,Fc*2/Fs); % Lowpass filter
figure(2) ;plot([num,den]) ;
title(Carrier signal) ;
y1 = ammod(s,Fc,Fs); % Modulate.
figure(3) ;plot(y1) ;
title(Modulated signal) ;
s1 = amdemod(y1,Fc,Fs,0,0,num,den); % Demodulate.
figure(4) ;plot(s1) ;
title(DeModulated signal) ;
OUTPUT:

Modulating signal

-1

-2

-3

100

200

300

400

500

600

700

800

900

Carrier signal

20
15
10
5
0
-5
-10
-15
-20

10

15

20

25

Modulated signal

-1

-2

-3

100

200

300

400

500

600

700

800

900

600

700

800

900

DeModulated signal

-1

-2

-3

100

200

300

400

500

%Freequency Modulation
%fmmod- Freequency Modulation
t = .01;
Fc = 10000; Fs = 80000;
t = [0:1/Fs:0.01]';
s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal
figure(1) ;plot(s) ;
title(Modulating signal) ;
[num,den] = butter(10,Fc*2/Fs); % Lowpass filter
figure(2) ;plot([num,den]) ;
title(Carrier signal) ;
dev=50;
y1 = ammod(s,Fc,Fs,dev); % Modulate.
figure(3) ;plot(y1) ;
title(Modulated signal) ;
.
OUTPUT:
Modulating signal

-1

-2

-3

100

200

300

400

500

600

700

800

900

Carrier signal

20
15
10
5
0
-5
-10
-15
-20

10

15

20

25

Modulated signal

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

100

200

300

400

500

600

700

800

900

%Phase Modulation
% Prepare to sample a signal for two seconds,
% at a rate of 100 samples per second.
Fs = 100; % Sampling rate
t = [0:2*Fs+1]'/Fs; % Time points for sampling
% Create the signal, a sum of sinusoids.
x = sin(2*pi*t) + sin(4*pi*t);
figure(1) ;plot(x) ;
Fc = 10; % Carrier frequency in modulation
phasedev = pi/2; % Phase deviation for phase modulation
y = pmmod(x,Fc,Fs,phasedev); % Modulate.
figure(2) ;plot(y) ;
title(Modulated signal);
z = pmdemod(y,Fc,Fs,phasedev); % Demodulate.
figure(3) ;plot(z) ;
title(Demodulated signal);
OUTPUT:

2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

50

100

150

200

250

200

250

Modulated signal

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

50

100

150

Unmodulated signal

2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

50

100

%PSK Modulation and Demodulation.


len = 10000; % Number of symbols
M = 16; % Size of alphabet
msg = randint(len,1,M); % Original signal
figure(1);plot(msg);
% Modulate using both PSK
txpsk = pskmod(msg,M);
figure(2);
plot(rxpsk); title('PSK Modulation Plot')
% Demodulate the received signals.
recovpsk = pskdemod(rxpsk,M);
figure(3);
plot(recovpsk); title('PSK DeModulation Plot');
OUTPUT:

150

200

250

15

10

1000

2000

3000

4000

5000

6000

7000

8000

0.4

0.6

9000 10000

PSK Modulation Plot

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.8

PSK DeModulation Plot

15

10

1000

2000

3000

4000

5000

6000

7000

8000

9000 10000

%Quantization
%partition- To specify a partition in MATLAB
%codebook- codebook tells the quantizer which common value to assign
%quantiz-Produce quantization index and quantized output value
%legend-Graph legend for lines and patches
t = [0:.1:2*pi]; % Times at which to sample the sine function
sig = sin(t); % Original signal, a sine wave
partition = [-1:.2:1]; % Length 11, to represent 12 intervals
codebook = [-1.2:.2:1]; % Length 12, one entry for each interval
[index,quants] = quantiz(sig,partition,codebook); % Quantize.
plot(t,sig,'x',t,quants,'.')
legend('Original signal','Quantized signal');
axis([-.2 7 -1.2 1.2]
OUTPUT:

Original signal
Quantized signal

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

S-ar putea să vă placă și