Genration of Elementry Signals: While

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 46

GENRATION OF ELEMENTRY SIGNALS

clear;
clc;
disp('select signal');
while(1)
A=input('1.unit impulse \n 2.unit step\n 3.ramp\n 4.damped sinusoid\n 5.discrete
expo\n 6.exit\nA=');
switch A
case 1% 'unit impulse'
n=-4:4;
del_n=[zeros(1,4) 1 zeros(1,4)];
subplot(3,1,1);
stem(n,del_n);
title('unit impulse');
n=-4:4;
del1_n=[zeros(1,2) 1 zeros(1,6)];
subplot(3,1,2);
stem(n,del1_n);
ylabel('amplitude');
n=-4:4;
del2_n=[zeros(1,6) 1 zeros(1,2)];
subplot(3,1,3);
stem(n,del2_n);
xlabel('time');
case 2%'unit step'
n=-4:4;
u_n=[zeros(1,4) ones(1,5)];
subplot(3,1,1);
stem(n,u_n);
title('unit step');
n=-4:4;
u1_n=[zeros(1,2) ones(1,7)];
subplot(3,1,2);
stem(n,u1_n);
ylabel('amplitude');
n=-4:4;
u2_n=[zeros(1,6) ones(1,3)];
subplot(3,1,3);
stem(n,u2_n);
xlabel('time');
case 3%'ramp'
n=0:10;

subplot(3,1,1);
stem(n);
title('ramp');
n=0:10;
subplot(3,1,2);
stem(n-2);
ylabel('amplitude');
n=0:10;
subplot(3,1,3);
stem(n+2);
xlabel('time');
case 4%'damped sinusoid'
n=0:0.5:10;
a=0.2;
y=exp(-(a*n)).*sin(n);
stem(n,y);
title('damped sinusoid');
ylabel('amplitude');
xlabel('time');
case 5%'discrete expo');
n=0:20;
a=0.5;
x=a.^n;
subplot(4,1,1);
stem(n,x);
title('discrete expo');
n=0:20;
a=2;
x=a.^n;
subplot(4,1,2);
stem(n,x);
n=0:20;
a=-0.5;
x=a.^n;
subplot(4,1,3);
stem(n,x);
n=0:20;
a=-1.1;
x=a.^n;
subplot(4,1,4);
stem(n,x);
xlabel('time');
case 6
break
end
end

OUTPUT:
select signal
1.unit impulse
2.unit step
3.ramp
4.damped sinusoid
5.discrete expo
6.exit
A=1

unit impulse
1

0.5

0
-4

-3

-2

-1

-3

-2

-1

-3

-2

-1

0
time

amplitude

0.5

0
-4
1

0.5

0
-4

select signal
1.unit impulse
2.unit step
3.ramp
4.damped sinusoid
5.discrete expo
6.exit
A=2
unit step
1

0.5

0
-4

-3

-2

-1

-3

-2

-1

-3

-2

-1

0
time

amplitude

0.5

0
-4
1

0.5

0
-4

select signal
1.unit impulse
2.unit step
3.ramp
4.damped sinusoid
5.discrete expo
6.exit
A=3
ramp
10

10

11

10

11

6
time

10

11

amplitude

10
5
0
-5
15
10
5
0

select signal
1.unit impulse
2.unit step
3.ramp
4.damped sinusoid
5.discrete expo
6.exit
A=4

damped sinusoid
1

0.8

amplitude

0.6

0.4

0.2

-0.2

-0.4

5
time

10

select signal
1.unit impulse
2.unit step
3.ramp
4.damped sinusoid
5.discrete expo
6.exit
A=5
discrete expo
1
0.5
0
2

0
2
6
x 10

10

12

14

16

18

20

10

12

14

16

18

20

10

12

14

16

18

20

10
time

12

14

16

18

20

1
0
1
0
-1
10
0
-10

GENERATION OF COMPLEX SIGNALS


clear;
clc;
disp('select signal');
while(1)
A=input('1.square \n 2.triangle \n 3.sawtooth \n 4.fullwave rectified waveform \n
5.parabola \n 6.impulse \n 7.exit \n A=');
switch A
case 1% square
clear;
clc;
N=input('enter the number of harmonics N=');
t=0:0.0001:10;
y=zeros(size(t));
for n=1:2:N
x=(4/pi)*(sin(n*t))/n;
y=y+x;
end
plot(t,y);
title('square waveform');
xlabel('time');
ylabel('output signal');
case 2% triangle
clear;
clc;
N=input('enter the number of harmonics N=');
t=0:0.0001:10;
y=zeros(size(t))
for n=1:1:N
a=(n-1)/2;
x=(((-1)^a)*sin(n*t)*8)/(n*n*pi*pi);
y=y+x;
end
plot(t,y);
title('triangle waveform');
xlabel('time');
ylabel('output signal');
case 3% sawtooth
clear;
clc;
N=input('enter the number of harmonics N=');

t=0:0.0001:10;
y=zeros(size(t))
for n=1:1:N
a=n+1;
x=(((-1)^a)*sin(n*t)*2)/(n*pi);
y=y+x;
end
plot(t,y);
title('sawtooth waveform');
xlabel('time');
ylabel('output signal');
case 4% fullwave rectified waveform
clear;
N=input('enter the number of harmonics N=');
t=0:0.0001:10;
y=zeros(size(t))
for n=1:1:N
a=((4*n*n)-1);
x=(4*cos(2*n*t))/(pi*a);
y=y+x;
end
y=(2/pi)-y;
plot(t,y);
title('fullwave rectified waveform');
xlabel('time');
ylabel('output signal');
case 5% parabola
clear;
clc;
N=input('enter the number of harmonics N=');
t=-1:0.0001:1;
y=zeros(size(t))
for n=1:1:N
a=n+1;
x=(2*((-1)^a)*cos(n*t))/(n*n);
y=y+x;
end
plot(t,y);
title('parabola waveform');
xlabel('time');
ylabel('output signal');
case 6% impulse
clear;
clc;
N=input('enter the number of harmonics N=');

t=-1:0.0001:1;
y=zeros(size(t))
for n=1:1:N
x=cos(n*t);
y=y+x;
end
plot(t,y);
title('impulse waveform');
xlabel('time');
ylabel('output signal');
case 7
break
end
end

OUTPUT
select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=1
square waveform
1.5

output signal

0.5

-0.5

-1

-1.5

5
time

10

select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=2
enter the number of harmonics N=500
triangle waveform
1
0.8
0.6

output signal

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

5
time

10

select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=3
enter the number of harmonics N=500
sawtooth waveform
1.5

output signal

0.5

-0.5

-1

-1.5

5
time

10

select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=4
enter the number of harmonics N=500
fullwave rectified waveform
1
0.9
0.8

output signal

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

5
time

10

select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=5
enter the number of harmonics N=500
parabola waveform
1.8

1.7

output signal

1.6

1.5

1.4

1.3

1.2

1.1
-1

-0.8

-0.6

-0.4

-0.2

0
time

0.2

0.4

0.6

0.8

select signal
1.square
2.triangle
3.sawtooth
4.fullwave rectified waveform
5.parabola
6.impulse
7.exit
A=6
enter the number of harmonics N=100

impulse waveform
100

80

output signal

60

40

20

-20

-40
-1

-0.8

-0.6

-0.4

-0.2

0
time

0.2

0.4

0.6

0.8

LINEAR AND CONVOLUTION OF TWO SEQUENCE


clc;
clear;
while(1)
A=input('1.linear convolution \n 2.circular convolution \n 3.exit \n enter your choice:');
switch A
case 1% linear convolution
clc;
clear;
a=input('enter x(n):');
b=input('enter h(n):');
n1=length(a);
n2=length(b);
N=n1+n2-1;
a1=[a zeros(1,(N-n1))];
b1=[b zeros(1,(N-n2))];
h=zeros(length(b1),length(b1));
for i=1:length(b1)
k=1;
for j=1:length(b1)
if j<i
h(j,i)=0;
else
h(j,i)=b1(k);
k=k+1;
end
end
end
disp(h);
y=(h*a1')';
disp(y);
stem(y);
title('linear convolution');
xlabel('n');
ylabel('y(n)');
case 2
clc;
clear;
a=input('enter x(n):');
b=input('enter h(n):');
n1=length(a);
n2=length(b);
N=max(n1,n2);
a1=[a zeros(1,(N-n1))];
b1=[b zeros(1,(N-n2))];
h=zeros(N,n2);
k=2;
for i=1:N
k=k-1;
for j=1:N

if(k==N+1)

k=1;
end
h(j,i)=b1(k);
k=k+1;
end
end
disp(h);
y=(h*a');
disp('the result is');
disp(y);
stem(y);
title('circular convoution');
xlabel('n');
ylabel('y(n)');
case 3
clc;
clear;
disp('INVALID OPTION');
end
end

OUTPUT
1.linear convolution
2.circular convolution
3.exit
enter your choice:1

enter x(n):[1 1 1 1]
enter h(n):[2 2]
2
0
0
0
2
2
0
0
0
2
2
0
0
0
2
2
0
0
0
2
2

0
0
0
0
2
2
linear convolution

4
3.5
3

y(n)

2.5
2
1.5
1
0.5
0

1.5

2.5

3
n

3.5

4.5

OUTPUT
1.linear convolution
2.circular convolution
3.exit
enter your choice:2

enter x(n):[1 1 1 1]
enter h(n):[2 2]
2
2
0
0

0
2
2
0

0
0
2
2

2
0
0
2

the result is
4
4
4
4
circular convoution
4
3.5
3

y(n)

2.5
2
1.5
1
0.5
0

1.5

2.5
n

3.5

DESIGN OF LOWPASS AND HIGHPASS FIR FILTER

while(1)
clc;
clear;
a=input('\n FIR FILTERS \n 1.lowpass \n 2.highpass \n 3.exit \n enter the
choice:');
if(a~=3)
win=input('choose the window \n 1.rectangular \n 2.hamming \n
3.hanning\n enter the choice:');
n=input('enter the order of the filter:');
end
switch a
case 1
fc=input('enter the cut off frequency:');
fs=input('enter the sampling frequency:');
wc=fc/(fs/2);
f=0:(fs/2);
switch win
case 1
w=rectwin(n);
case 2
w=hamming(n);
case 3
w=hann(n);
end
b=fir1(n-1,wc,w);
h=freqz(b,1,f,fs);
mag_h=20*log10(abs(h));
pha_h=unwrap(angle(h));
subplot(2,1,1),plot(f,pha_h);
xlabel('frequency');
ylabel('phase');
title('phase plot');
subplot(2,1,2),plot(f,mag_h);
xlabel('frequency');
ylabel('magnitude');
title('magnitude plot');
case 2
fc=input('enter the cut off frequency:');
fs=input('enter the sampling frequency:');
wc=(fc/(fs/2));
f=0:(fs/2);
switch win
case 1

w=rectwin(n);
case 2
w=hamming(n);

case 3
w=hann(n);
end
b=fir1(n-1,wc,'high',w);
h=freqz(b,1,f,fs);
mag_h=20*log10(abs(h));
pha_h=unwrap(angle(h));
subplot(2,1,1),plot(f,pha_h);
xlabel('frequency');
ylabel('phase');
title('phase plot');
subplot(2,1,2),plot(f,mag_h);
xlabel('frequency');
ylabel('magnitude');
title('magnitude plot');
case 3
clc;
break
end
end

OUTPUT
FIR FILTERS
1.lowpass
2.highpass
3.exit
enter the choice:1
choose the window
1.rectangular
2.hamming
3.hanning
enter the choice:1
enter the order of the filter:31
enter the cut off frequency:2500
enter the sampling frequency:8000

phase plot
0

phase

-10
-20
-30
-40

500

1000

1500

2000
2500
frequency
magnitude plot

500

1000

1500

3000

3500

4000

3000

3500

4000

magnitude

50

-50

-100

2000
frequency

2500

FIR FILTERS
1.lowpass
2.highpass
3.exit
enter the choice:2
choose the window
1.rectangular
2.hamming
3.hanning
enter the choice:2
enter the order of the filter:31
enter the cut off frequency:2500
enter the sampling frequency:8000
phase plot
10

phase

0
-10
-20
-30

500

1000

1500

2000
2500
frequency
magnitude plot

500

1000

1500

3000

3500

4000

3000

3500

4000

magnitude

100

-100

-200

2000
frequency

2500

DESIGN OF BANDPASS AND BANDSTOP FILTER

while(1)
clc;
clear;
a=input('\n FIR FILTERS \n 1.bandpass \n 2.bandstop \n 3.exit \n enter the
choice:');
if(a~=3)
win=input('choose the window \n 1.rectangular \n 2.hamming \n
3.hanning\n enter the choice:');
n=input('enter the order of the filter:');
end
switch a
case 1
fc1=input('enter the cut off frequency 1:');
fc2=input('enter the cut off frequency 2:');
fs=input('enter the sampling frequency:');
wc1=fc1/(fs/2);
wc2=fc2/(fs/2);
wc=[wc1 wc2];
f=0:(fs/2);
switch win
case 1
w=rectwin(n);
case 2
w=hamming(n);
case 3
w=hann(n);
end
b=fir1(n-1,wc,'bandpass',w);
h=freqz(b,1,f,fs);
mag_h=20*log10(abs(h));
pha_h=unwrap(angle(h));
subplot(2,1,1),plot(f,pha_h);
xlabel('frequency');
ylabel('phase');
title('phase plot');
subplot(2,1,2),plot(f,mag_h);
xlabel('frequency');
ylabel('magnitude');
title('magnitude plot');
case 2

fc1=input('enter the cut off frequency 1:');


fc2=input('enter the cut off frequency 2:');
fs=input('enter the sampling frequency:');
wc1=fc1/(fs/2);
wc2=fc2/(fs/2);
wc=[wc1 wc2];
f=0:(fs/2);
switch win
case 1
w=rectwin(n);

case 2

w=hamming(n);
case 3
w=hann(n);

end
b=fir1(n-1,wc,'stop',w);
h=freqz(b,1,f,fs);
mag_h=20*log10(abs(h));
pha_h=unwrap(angle(h));
subplot(2,1,1),plot(f,pha_h);
xlabel('frequency');
ylabel('phase');
title('phase plot');
subplot(2,1,2),plot(f,mag_h);
xlabel('frequency');
ylabel('magnitude');
title('magnitude plot');
case 3
clc;
break
end
end

OUTPUT
FIR FILTERS
1.bandpass
2.bandstop
3.exit
enter the choice:1
choose the window
1.rectangular
2.hamming
3.hanning
enter the choice:3
enter the order of the filter:31
enter the cut off frequency 1:1000
enter the cut off frequency 2:4000
enter the sampling frequency:10000

phase plot
20

phase

-20

-40

500

1000

1500

2000

2500 3000
frequency
magnitude plot

500

1000

1500

2000

3500

4000

4500

5000

3500

4000

4500

5000

magnitude

50

-50

-100

2500 3000
frequency

FIR FILTERS
1.bandpass
2.bandstop
3.exit
enter the choice:2
choose the window
1.rectangular
2.hamming
3.hanning
enter the choice:1
enter the order of the filter:31
enter the cut off frequency 1:1000
enter the cut off frequency 2:4000
enter the sampling frequency:10000

phase plot
0

phase

-10

-20

-30

500

1000

1500

2000

2500 3000
frequency
magnitude plot

500

1000

1500

2000

3500

4000

4500

5000

3500

4000

4500

5000

magnitude

50

-50

-100

2500 3000
frequency

IIR FILTER USING IMPULSE INVARIANT TECHNIQUE


fp=input('fp');
fs=input('fs');
rp=input('pass band ripple');
rs=input('stop band ripple');
f=input('sampling f');
wp=2*pi*fp/f;
ws=2*pi*fs/f;
wc=(wp+ws)/2;
disp(wc);

[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn);
[bz,az]=impinvar(b,a);
freqz(bz,az);

OUTPUT:
fp 500
fs 1000
pass band ripple 3
stop band ripple 10
sampling f10000
0.4712

Magnitude (dB)

20

10

-10

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)

400
300
200
100
0

SAMPLING
N=input('enter no of samples');
fm=input('enter signal frequency');
t=0:.0001:(1/fm);
y=sin(2*pi*fm*t);
subplot(1,2,1);plot(t,y);
title('input signal');
xlabel('t');
ylabel('amp');
fs=fm*N;
n=0:N-1;
z=sin(2*pi*fm*n/fs);

subplot(1,2,2);
stem(n,z);
title('sampled signal');
xlabel('n');ylabel('amp');

OUTPUT
enter no of samples 20
enter signal frequency 100

sampled signal
1

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2
amp

amp

input signal
1

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

0.005
t

0.01

-1

BILINEAR TRANSFORMATION
fp=input('fp');
fs=input('fs');
rp=input('pass band ripple');
rs=input('stopband ripple');
f=input('sampling f');

10
n

15

20

wp=2*pi*fp;
ws=2*pi*fs;
wc=(wp+ws)/2;
disp(wc);
[n,wn]=buttord(wp,ws,rp,rs,'s');
[b,a]=butter(n,wn,'s');
[bz,az]=bilinear(b,a,f);
freqz(bz,az);

fp 500
fs 1000
pass band ripple 3
stopband ripple 10
sampling f 10000

phase plot

Magnitude (dB)

-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)

0
-50
-100
-150
-200

SPECTRUM ESTIMATION
N=32;
fm=input('enter i/p frequency');
fs=input('enter sampling frequency ');

a=input('enter amplitude');
t=0:.001:(1/fm);
y=a*sin(2*pi*fm*t);
n=0:N-1;
z=a*sin(2*pi*fm*n/fs);
o=fs/N;
p=fft(z,N);
q=rem(fm,o);
if(q==0)
disp('no spectral leakage');
else
disp('spectral leakage');
end;
m=0:N-1;
stem(m,abs(p));
title('spectrum estimation');
xlabel('k');
ylabel('abs(x(k))');

OUTPUT:
enter i/p frequency 10

enter sampling frequency 72


enter amplitude 1
spectral leakagel leakage
spectrum estimation
12

10

abs(x(k))

10

15

20
k

OUTPUT:
enter i/p frequency 10
enter sampling frequency 64
enter amplitude 1

25

30

35

no spectral leakage
spectrum estimation
16
14
12

abs(x(k))

10
8
6
4
2
0

10

15

20
k

DIT - FFT
data=input('Enter input sequence')
a=length(data);
for u=1:5

25

30

35

if((2^u)>=a)
t1=2^u;
t2=u;
break;
end
end
X2=[data zeros(1,t1-a)];
X=bitrevorder(X2);
N=t1;L=log2(N);
k1=2;
k2=N/2;
k3=1;
for i1=1:L
L1=1;
for i2=1:k2
k=1;
for i3=1:k3
i=i3+L1-1;
j=i+k3;
Y=-2*pi*sqrt(-1)*(k-1)/N;
W=exp(Y);
T=X(j)*W;
X(j)=X(i)-T;X(i)=X(i)+T;
k=k+k2;
end
L1=L1+k1;
end
k1=k1*2;
k2=k2/2;
k3=k3*2;
end
disp(X);
k=0:N-1;
subplot(2,2,1);stem(k,real(X));
xlabel('K');ylabel('Real(X(K))');
title('Real(X(K))');
subplot(2,2,2);stem(k,imag(X));
xlabel('K');ylabel('Imag(X(K))');
title('Imag(X(K))');

DIT FFT :
Sample I / O :
Enter input sequence [1 1 1 1 1 1 0 0]

Imag(X(K))
2

Imag(X(K))

Real(X(K))

Real(X(K))
6

2
0
-2

4
K

0
-1
-2

DIF FFT
X1=input('Enter i/p sequence');
a=length(X1);

4
K

for u=1:5
if((2^u)>=a)
u1=2^u;
u2=u;
break;
end;
end;
X=[X1 zeros(1,u1-a)];
N=u1;
L=log2(N);
k1=N;k2=1;k3=N/2;
for i1=1:L
L1=1;
for i2=1:k2
k=1;
for i3=1:k3
i=i3+L1-1;
j=i+k3;
YY=-2*pi*sqrt(-1)*(k-1)/N;
W=exp(YY);
t1=X(i)-X(j);t2=X(i)+X(j);
X(i)=t2;
X(j)=t1*W;
k=k+k2;
end;
L1=L1+k1;
end;
k1=k1/2;
k2=k2*2;
k3=k3/2;
end;
X2=bitrevorder(X);
k=0:N-1;
subplot(2,2,1);stem(k,real(X2));
xlabel('K');ylabel('Real(X(K))');
title('Real(X(K))');
subplot(2,2,2);stem(k,imag(X2));
xlabel('K');ylabel('Imag(X(K))');
title('Imag(X(K))');
disp(X2);

OUT PUT
Enter i/p sequence[0 2 4 6]

Im ag(X(K))
4

10

2
Imag(X(K))

Real(X(K))

Real(X(K))
15

5
0
-5

0
-2
-4

2
K

ADDITION

INP1
.SET 0H
INP2
.SET 1H
OUT
.SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
ADD INP2,A
STL A,OUT
HLT: B
HLT

Input:
Data Memory:
A000H
A001H

0004H
0004H

Output:
Data Memory:
A002H

0008H

DIVISION
DIVID
DIVIS
OUT

.SET 0H
.SET 1H
.SET 2H
.mmregs

.text
START:
STM #140H,ST0
RSBX CPL
RSBX FRCT
NOP
NOP
NOP
NOP
LD DIVID,A
RPT #0FH
SUBC DIVIS,A
STL A,OUT
HLT: B
HLT
.END

INPUT:
DATA MEMORY
A000H
000AH
A001H
0002H

OUTPUT:
A002H

0005H

MULTIPLICATION
.mmregs
.text

START:
STM
STM
STM
ST
LD
ST
MPY
STL
HLT: B
.END

#0140H,ST0
#40H,PMST
#0A000H,AR0
#1H,*AR0
*AR0+,T
#2H,*AR0
*AR0+,A
A,*AR0
HLT

INPUT:
DATA MEMORY
A000H
1234H
A001H
1234H
OUTPUT:
DATA MEMORY
A0002H
5A90H
A0003H
014BH

SUBTRACTION
INP1
.SET 0H
INP2
.SET 1H
OUT
.SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
SUB INP2,A
STL A,OUT
HLT: B
HLT

Input:
Data Memory:
A000H
A001H

0004H
0002H

Output:
Data Memory:
A002H

0002H

You might also like