Experiment 2-Generation of Signals
Experiment 2-Generation of Signals
subplot(222),plot2d(abs(y));xtitle('IFFT of x');
subplot(223),plot2d(yr);xtitle('Real Component');
subplot(224),plot2d(yi);xtitle('Imaginary Component');
Experiment 5- Convolution
clf;
t=0:.1:10;
x=sin(t)+sin(3*t)/3; //create a signal
//x=squarewave(1:10);
//h=[1 1 -1 1 -1 1 -1 1 -1 1 1 -1 1 -1 1];
//h=ones(1,60);
h=rand(1,60); //create a convolving function
//h=mtlb_repmat([1 -1],[1 20]);
y=convol(h,x);
subplot(311),plot2d3(x);xtitle('signal');xaxis(0,[2 2],[2 0.1 3],[-4 0]);
subplot(312),plot2d3(h);xtitle('window');
subplot(313),plot2d3(y);xtitle('Convolved result');
Experiment 6- Auto correlation and cross correlation
clc
t=0:0.1:10;
sn=.3*sin(%pi*t)+sin(4*%pi*t); //create an input signal
n=size(t,2);
// create different functions to study the effects of correlation
rn=rand(1,n); //random signal
sn1=-sn; // inverted form of input signal
sn2=.3*sin(%pi*t+%pi/4)+sin(4*%pi*t+%pi/4); // time-shifted input
signal
sn_ns=sn+.5*rn; //input signal with random noise
sn_smpl=sn_ns(1,3:22); // sample of input signal with noise
n1=size(sn_smpl,2);
cr1=0; // auto correlation of input
cr2=0; // auto correlation of noise
cr3=0; // cross correlation of input with noise
cr4=0; // cross correlation of input with its inverted form
cr5=0; // cross correlation of input with time-shifted input signal
for i=1:n,
cr1=cr1+sn(1,i)*sn(1,i);
cr2=cr2+rn(1,i)*rn(1,i);
cr3=cr3+sn(1,i)*rn(1,i);
cr4=cr4+sn(1,i)*sn1(1,i);
cr5=cr5+sn(1,i)*sn2(1,i);
end
cr_cor=zeros(1,n);
cr_cor1=cr_cor;
cr_cor2=cr_cor;
cr_cor3=cr_cor;
cr_cor4=cr_cor;
for i=1:n-n1,
for j=0:n1-1,
cr_cor(1,i)=cr_cor(1,i)+sn(1,i+j)*sn_smpl(1,j+1);
cr_cor1(1,i)=cr_cor1(1,i)+rn(1,i+j)*sn_smpl(1,j+1);
cr_cor2(1,i)=cr_cor2(1,i)+sn2(1,i+j)*sn_smpl(1,j+1);
cr_cor3(1,i)=cr_cor3(1,i)+sn1(1,i+j)*sn_smpl(1,j+1);
cr_cor4(1,i)=cr_cor4(1,i)+sn_ns(1,i+j)*sn_smpl(1,j+1);
end
end
clc
printf('\nCorrelation of sin with itself:\n');
cr1n=cr1/n
printf('\nCorrelation of random with itself:\n');
cr2n=cr2/n
printf('\nCorrelation of input with randomn:\n');
cr3n=cr3/n
printf('\nCorrelation of input with its inverted form:\n');
cr4n=cr4/n
printf('\nCorrelation of input with time_shifted input:\n');
cr5n=cr5/n
clf
subplot(431),plot(sn);
title('signal');
subplot(432),plot(rn);
title('random noise');
subplot(433),plot(sn1);
title('inverted signal');
subplot(434),plot(sn2);
title('time-shifted signal');
subplot(435),plot(sn_ns);
title('noisy signal')
subplot(436),plot(cr_cor);
title('autocorrelation of signal');
subplot(437),plot(cr_cor1);
title('correlation of signal with noise');
subplot(438),plot(cr_cor2);
title('correlation of signal with time-shifted signal');
subplot(439),plot(cr_cor3);
title('correlation of signal with inverted signal');
subplot(4,3,10),plot(cr_cor4);
title('correlation of signal with noisy signal');
Experiment 7- Linearity and time-invariance
//Testing LTI Properties Superposition Principle, Time Invariance
clf;
t=0:0.1:10;
x=sin(t)+sin(3*t)/3; //create an input signal
h=1;
if nz>0,
for i=1:nz,
zr(i)=input('Enter position of zero:');
hz=hz*(s-zr(i)); // create the polynomial using each zero position
information, if any
end;
end;
h=real(hz)/real(hp); // create the transfer function
h1=syslin('c',h); // create the system
plzr(h1); // plot pole-zero positions on s-plane
xgrid;
else
disp('Minimum one pole is a must for any system...');
end;
Experiment 13- Gaussian noise
//Expt 13 Generation of Gaussian noise
mn=0; // set the value of mean
sig=2.5; // set the value of the variance
x=-10:0.1:10;
a=2.5; // set initial values of a, b and m to generate the pseudorandom sequence
b=-0.02;
m=5;
x1(1,1)=x(1,201); //initiate the first number of the sequence
for i=2:201, //pseudo random no. generation
x1(1,i)=modulo (a*x1(1,i-1)+b,m); // Linear Congruent pseudo random
number generator
end;
gsn=exp(-(x-mn)^2/(2*sig^2)); //Gaussian
subplot(221),plot(x,gsn);
gsn1=exp(-(x1-mn)^2/(2*sig^2)); // Linear Congruent pseudo random
Gaussian noise generator
subplot(222),plot(abs(gsn1));
mn1=mean(gsn1) // compute the mean of the sequence
v1=variance(gsn1) // compute the variance of the sequence
skw1=exp(-(gsn1-mn1)^3/v1^3); // compute the skew of the sequence
subplot(223),plot(abs(skw1));
krts1=exp(-(gsn1-mn1)^4/v2^4); // compute the Kurtosis of the
sequence
subplot(224),plot(abs(krts1));
y1=sum(skw1)
R1=sum(krts1)
Experiment 14- Sampling theorem
mtlb_close all;
clf;
t=0:0.1:10;
nt=size(t,2);
n=10;
fs=28; //sampling frequency
fm=3; //signal frequency
sn=sin(fm*t); //signal creation
sn_sp=fft(sn); // signal spectrum
sq=(1+squarewave(fs*t))/2; // sampling signal
sq_sp=fft(sq); // sampling signal spectrum
smpl=sn.*sq; // sampling of the signal
smpl_sp=fft(smpl); //spectrum of the sampled signal
sn1=smpl_sp;
sn1(n:nt-n)=0; // low-pass filtering
sn2=ifft(sn1); // time-domain of the filtered sampled signal
(Reconstructed signal)
subplot(4,2,2),plot(abs(sn_sp));xtitle('Input signal spectrum');
subplot(4,2,4),plot(abs(sq_sp));xtitle('Sampling signal spectrum');
subplot(4,2,6),plot(abs(smpl_sp));xtitle('Sampled Input signal
spectrum');
subplot(4,2,1),plot(sn);xtitle('Input Signal');
mtlb_axis([0 120 -2 2]);
subplot(4,2,3),plot(sq);xtitle('Sampling Signal');
mtlb_axis([0 120 -2 2]);
subplot(4,2,5),plot(smpl);xtitle('Sampled Input Signal');
mtlb_axis([0 120 -2 2]);
subplot(4,2,7), plot((sn2));xtitle('Reconstructed Signal');
subplot(4,2,8),plot(sn1);xtitle('Reconstructed Signal spectrum');
Experiment 15- Noise removal by correlation
// this program shows the use of correlation
// in removing noise from the signal
mtlb_close all;
clc;
clf;
t=0:.1:50;
nt=size(t,2);
sq=sin(4*t)+0.5*rand(1,nt); // signal with noise
sn1=rand(t); //random signal
sn=sn1(1,round(0.11*nt):round(0.14*nt)); // sample of random signal
smpl=sq(1,round(0.11*nt):round(0.14*nt)); // sample of input noisy
signal
n1=size(sn,2);
nn=n+2*(n1-1);
plot(sq);
set('current_figure',1);
plot(smpl);
cor=zeros(1,nt);
cor1=cor;
for i=1:nt-n1,
sm=0;
sm1=0;
for j=1:n1,
sm=sm+sq(i+j-1)*smpl(j); // correlation of input with its sample
sm1=sm1+sq(i+j-1)*sn(j); // correlation of input with sample of
random signal
end;
cor(i)=sm;
cor1(i)=sm1;
end;
set('current_figure',2);
subplot(211),plot(cor); xtitle('filtered signal');
subplot(212),plot(cor1);xtitle('filtered noise');
disp('peak to peak of filtered signal:');
mx1=max(cor)-min(cor)
disp('peak to peak of filtered noise:');
mx2=max(cor1)-min(cor1)
Experiment 16- Extraction of periodic signal from noisy signal
/ this program shows the use of correlation
// in extracting the signal
mtlb_close all;
clc;
clf;
t=0:.1:50;
nt=size(t,2);
sq=sin(4*t)+0.5*rand(1,nt); // signal with noise
sn1=rand(t);
sn=sn1(1,round(0.11*nt):round(0.14*nt)); //
smpl=sq(1,round(0.11*nt):round(0.14*nt)); // sample of input noisy
signal
n1=size(sn,2);
nn=n+2*(n1-1);
plot(sq);
set('current_figure',1);
plot(smpl);
cor=zeros(1,nt);
cor1=cor;
for i=1:nt-n1,
sm=0;
sm1=0;
for j=1:n1,
sm=sm+sq(i+j-1)*smpl(j);
sm1=sm1+sq(i+j-1)*sn(j);
end;
cor(i)=sm;
cor1(i)=sm1;
end;
set('current_figure',2);
subplot(211),plot(cor); xtitle('filtered signal');
subplot(212),plot(cor1);xtitle('filtered noise');
disp('peak to peak of filtered signal:');
mx1=max(cor)-min(cor)
disp('peak to peak of filtered noise:');
mx2=max(cor1)-min(cor1)
Experiment 17- Weiner-Khinchine relations
// Wiener-Khinchin theorem
// The power spectral density of wide-sense stationary process
// is the Fourier transform of the corresponding
// Auto-correlation fn.
clc;
clf;
t=0:0.1:10;
//x=squarewave(2*t);
x=rand(1,101);
y=sin(t);
sm1=cspect(100,100,'tr',x);
sm2=cspect(100,100,'tr',y);
sm3=cspect(100,100,'tr',x,y);
subplot(321),plot(sm1);xtitle('random signal');
subplot(322),plot(sm2);xtitle('sine signal');
subplot(323),plot(sm3);xtitle('cross-correlation');
[cov,mn]=corr(x,101);
subplot(324),bar(cov);xtitle('Auto-correlation');
sm4=fft(cov);
sm4m=abs(sm4);
subplot(325),plot(2*t,sm4m,'r',t,sm1(1,1:101),'b')
xtitle('ft of auto-correlation-red power spectral density blue');
Experiment 18- Random process stationarity checking
// Expt 18 Wide-sense stationarity checking of stationary process
// means at different times remain same m(t1)=m(t2)
//variances measured between two intervals depends only on the
difference between the intervals and
// not on the interval itself v(t1)-v(t2)=v(t1+)-v(t2+)
clc
x=rand(1,500); //random signal output of a random process
mx=mean(x) //mean of this whole sequence
// sample creation around different instants
x1=x(1,26:50);
x2=x(1,76:100);
x3=x(1,126:150);
x4=x(1,226:250);
m1=mean(x1) // mean of
m2=mean(x2) // mean of
m3=mean(x3) // mean of
m4=mean(x4) // mean of
v12=variance([m1 m2])
v13=variance([m1 m3])
v14=variance([m1 m4])
v34=variance([m3 m4])
0 10 20 30 40 50 60 70 80 90 100
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
Impulse
0 10 20 30 40 50 60 70 80 90 100
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
Step input
0 10 20 30 40 50 60 70 80 90 100
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Square wave
0 10 20 30 40 50 60 70 80 90 100
1
2
3
4
5
6
7
samples
samples
samples
samples
around
around
around
around
t1
t2
t3
t4
8
9
10
Sawtooth
0 10 20 30 40 50 60 70 80 90 100
0123456789
10
Triangular
0 10 20 30 40 50 60 70 80 90 100 110
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Sine
0 10 20 30 40 50 60 70 80 90 100
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Multiplication
0 10 20 30 40 50 60 70 80 90 100
15
10
5
0
5
10
15
Addition
0 20 40 60 80 100 120
0123456789
10
Timeshifted
0 10 20 30 40 50 60 70 80 90 100 110
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Time Scaled
0 10 20 30 40 50 60 70 80 90 100 110
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Original sine
0 10 20 30 40 50 60 70 80 90 100
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Square wave
0 10 20 30 40 50 60 70 80 90 100
123456789
10
folded Sawtooth
0 20 40 60 80100120140160180200220
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Signal 1 (Odd)
0 20 40 60 80100120140160180200220
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
Signal 2 (even)
0 20 40 60 80100120140160180200220
1.5
1.0
0.5
0.0
0.5
1.0
1.5
Sum of 1 and 2
0 20 40 60 80100120140160180200220
1.5
1.0
0.5
0.0
0.5
1.0
1.5
Time inverted sum signal
0 20 40 60 80100120140160180200220
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Even Component
0 20 40 60 80100120140160180200220
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Odd component
0 20 40 60 80100120140160180200220
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
0.04
0.06
0.08
Imaginary Component
0 10 20 30 40 50 60 70 80 90100110
1.5
1.0
0.5
0.0
0.5
1.0
1.5
signal
0 10 20 30 40 50 60 70 80 90100110
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
random noise
0 10 20 30 40 50 60 70 80 90100110
1.5
1.0
0.5
0.0
0.5
1.0
1.5
inverted signal
0 10 20 30 40 50 60 70 80 90100110
1.5
1.0
0.5
0.0
0.5
1.0
1.5
timeshifted signal
0 10 20 30 40 50 60 70 80 90100110
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
noisy signal
0 10 20 30 40 50 60 70 80 90100110
10
5
0
5
10
15
autocorrelation of signal
0 10 20 30 40 50 60 70 80 90100110
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
correlation of signal with noise
0 10 20 30 40 50 60 70 80 90100110
15
10
5
0
5
10
correlation of signal with timeshifted signal
0 10 20 30 40 50 60 70 80 90100110
15
10
5
0
5
10
correlation of signal with inverted signal
0 10 20 30 40 50 60 70 80 90100110
10
5
0
5
10
15
correlation of signal with noisy signal
0 10 20 30 40 50 60 70 80 90 100 110
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
original
0 20 40 60 80 100 120 140
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
shifted
0 10 20 30 40 50 60 70 80 90 100
0.20
0.15
0.10
0.05
0.00
0.05
0.10
0.15
0.20
original response
0 20 40 60 80 100 120
0.20
0.15
0.10
0.05
0.00
0.05
0.10
0.15
0.20
shifted response
0 20 40 60 80 100 120
0.20
0.15
0.10
0.05
0.00
0.05
0.10
0.15
0.20
original response shifted
0 20 40 60 80 100 120
1.0
0.8
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
1.0
0.2
0.0
0.2
0.4
0.6
0.8
1.0
signal 1+2
0 10 20 30 40 50 60 70 80 90 100
0.20
0.15
0.10
0.05
0.00
0.05
0.10
0.15
0.20
output 1+ output 2
0 10 20 30 40 50 60 70 80 90 100
0.20
0.15
0.10
0.05
0.00
0.05
0.10
0.15
0.20
output (1+2)
2.0 ZPeorleo1s.5
0.6
0.4
0.2
0.0
0.2
0 100 200 300 400 500 600 700 800 900 1000
0
2
4
6
8
10
12
14
16
18
20
0 5 10 15 20 25 30 35 40 45 50
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
0 100 200 300 400 500 600 700 800 900 1000
0
2
4
6
8
10
12
14
16
18
20
0 5 10 15 20 25 30 35 40 45 50
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
0 100 200 300 400 500 600 700 800 900 1000
0
2
4
6
8
10
12
14
16
18
20
0 5 10 15 20 25 30 35 40 45 50
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
0 10 20 30 40 50 60 70 80 90 100 110
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
0 10 20 30 40 50 60 70 80 90 100 110
5
0
5
10
15
20
25
30
35
0 10 20 30 40 50 60 70 80 90 100 110
800
700
600
500
400
300
200
100
0
100
ZPeorleos
0.5
0.0
0.5
1.0
1.5
transmission zeros and poles
real axis
imag. axis
10 8 6 4 2 0 2 4 6 8 10
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
0 20 40 60 80 100 120 140 160 180 200 220
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
0 20 40 60 80 100 120 140 160 180 200 220
0e+000
1e+212
2e+212
3e+212
4e+212
5e+212
6e+212
7e+212
8e+212
0 20 40 60 80 100 120 140 160 180 200 220
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
0 10 20 30 40 50 60 70 80 90 100 110
05
10
15
20
25
30
35
40
45
50
Input signal spectrum
0 10 20 30 40 50 60 70 80 90 100 110
0
10
20
30
40
50
60
Sampling signal spectrum
0 10 20 30 40 50 60 70 80 90 100 110
0
5
10
15
20
25
Sampled Input signal spectrum
0 20 40 60 80 100 120
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Input Signal
0 20 40 60 80 100 120
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Sampling Signal
0 20 40 60 80 100 120
2.0
1.5
1.0
0.5
0.0
0.5
1.0
1.5
2.0
Sampled Input Signal
0 10 20 30 40 50 60 70 80 90 100 110
0.6
0.4
0.2
0.0
0.2
0.4
0.6
0.8
Reconstructed Signal
0 10 20 30 40 50 60 70 80 90 100 110
0
5
10
15
20
25
Reconstructed Signal spectrum
0 50 100 150 200 250 300 350 400 450 500 550
1.0
0.5
0.0
0.5
1.0
1.5
0 5 10 15 20 25 30 35 40 45
1.0
0.5
0.0
0.5
1.0
1.5
0 50 100 150 200 250 300 350 400 450 500 550
20
15
10
5
0
5
10
15
20
25
filtered signal
0 50 100 150 200 250 300 350 400 450 500 550
0
1
2
3
4
5
6
7
filtered noise
0 20 40 60 80 100 120 140 160 180 200
0.00
0.05
0.10
0.15
0.20
0.25
random signal
0 20 40 60 80 100 120 140 160 180 200
0
5
10
15
sine signal
0 20 40 60 80 100 120 140 160 180 200
0.00
0.05
0.10
0.15
0.20
0.25
0.30
crosscorrelation
12345678191101121314151617282920212232425262738393031323343536374849404142434
45464758595051525354556576869606162636465667879707727374757677889808182838485
8687989909192939495916917809001
0.02
0.01
0.00
0.01
0.02
0.03
0.04
0.05
0.06
0.07
Autocorrelation
0 2 4 6 8 10 12 14 16 18 20
0.00
0.05
0.10
0.15
0.20
0.25
ft of autocorrelationred power psectral density blue
ft
psd