Sei sulla pagina 1di 53

M.

Tech (DECS)-II SEM (2012 admitted batch)

ADVANCED SIGNAL PROCESSING LABORATORY

CYCLE-I: SIGNAL PROCESSING

1) DISRETE TIME SIGNALS AND SYSTEMS


2) Z- TRANSFORMS
3) DISCRETE FOURIER TRANSFORM AND ITS PROPERTIES
4) FIR Filter design
5) IIR Filter design
6) Applications of adaptive filtering

Ex:1- Discrete time signals and systems

1(a) representation of disrete time signal and its shifted signal

clc;
clear all;
close all;
x=[-1 1 2 3];
n=-2:1:1;
l=1;
m=n+l;
stem(n,x)
xlabel('n')
ylabel('x')
title('12481D3829 original signal')

figure, stem(m,x)
xlabel('m')
ylabel('x')
title('12481D3829 shifted signal')
output waveforms:
12481D3829 original signal
3

2.5

1.5

1
x

0.5

-0.5

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

12481D3829 shifted signal


3

2.5

1.5

1
x

0.5

-0.5

-1
-1 -0.5 0 0.5 1 1.5 2
m
1(b) representation of disrete time signal and its scaling and reverse
signal

clc;
clear all;
close all;
x=[1 -1 2 3]
n=-1:1:2
l=2
m=n*l
subplot(2,2,1)
stem(n,x)
xlabel('n')
ylabel('x')
title('12481D3829 original signal x(n)')
subplot(2,2,2)
stem(m,x)
xlabel('m')
ylabel('x')
title('12481D3829 scaling signal x(n/2)')
p=n/l
subplot(2,2,3)
stem(p,x)
xlabel('p')
ylabel('x')
title('12481D3829 scaling signal x(2n)')
q=-n
subplot(2,2,4)
stem(q,x)
xlabel('q')
ylabel('x')
title('12481D3829 reversal signal x(-n)')
output waveforms:

12481D3829 original signal x(n) 12481D3829 scaling signal x(n/2)


3 3

2 2

1 1
x

x
0 0

-1 -1
-1 0 1 2 -2 0 2 4
n m
12481D3829 scaling signal x(2n) 12481D3829 reversal signal x(-n)
3 3

2 2

1 1
x

0 0

-1 -1
-0.5 0 0.5 1 -2 -1 0 1
p q

1(c) representation of disrete time signal and high pass filter

clc;
clear all;
close all;
x=[1 -1 2 3]
n=-1:1:2
b=[1 -1]
a=[1]
y=filter(b,a,x)
stem(n,x)
xlabel('n')
ylabel('x')
title('12481D3829 original signal')
figure,stem(n,y)
stem(n,y)
xlabel('n')
ylabel('y')
title('12481D3829 highpass filter')
output waveforms:
12481D3829 original signal
3

2.5

1.5

1
x

0.5

-0.5

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

12481D3829 highpass filter


3

2.5

1.5

0.5
y

-0.5

-1

-1.5

-2
-1 -0.5 0 0.5 1 1.5 2
n
Ex:2- z-transforms

2(a z-transform

clc;
clear all;
close all;
syms n a w real
syms z complex
x=1^n
disp('z-transform of 1^n')
ztrans(x)

y=a^n
disp('z-transform of a^n')
ztrans(y)
p=cos(w*n)
disp('z-transform of cos(w*n)')
ztrans(p)
q=sin(w*n)
disp('z-transform of sin(w*n)')
ztrans(q)
iztrans(x,z,n)
simplify(x)
iztrans(y,z,n)
simplify(y)
iztrans(p,z,n)
simplify(p)
iztrans(q,z,n)
simplify(q)

2(b) representation of unit impulse , unit step, and unit ramp signals

clc;
clear all;
close all;
n=-10:1:10
x1=1
x2=0
x=x1.*(n==0)+x2.*(n~=0)
subplot(3,1,1)
stem(n,x)
xlabel('n')
ylabel('x(n)')
title('12481D3829 unit impulse signal')
x1=1
x2=0
x=x1.*(n>=0)+x2.*(n<0)
subplot(3,1,2)
stem(n,x)
xlabel('n')
ylabel('x(n)')
title('12481D3829 unitstep signal')
x1=n
x2=0
x=x1.*(n>=0)+x2.*(n<0)
subplot(3,1,3)
stem(n,x)
xlabel('n')
ylabel('x(n)')
title('12481D3829 unitramp signal')

output waveforms:

12481D3829 unit impulse signal


1

0.5
x(n)

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
12481D3829 unitstep signal
1
x(n)

0.5

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
12481D3829 unitramp signal
10
x(n)

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
2(c) impulse response of fir and iir systems

a) Fir system
clc;
clear all;
close all;
n=0:1:6
x=[1,zeros(1,6)]
b=[1 -3]
a=[1]
y=filter(b,a,x)
subplot(2,1,1)
stem(n,x)
xlabel('n')
ylabel('x')
title('12481D3829 impulse signal')
subplot(2,1,2)
stem(n,y)
xlabel('n')
ylabel('y')
title('12481D3829 impulse response of fir system')

output waveforms:

12481D3829 impulse signal


1

0.5
x

0
0 1 2 3 4 5 6
n
12481D3829 impulse response of fir system
1

-1
y

-2

-3
0 1 2 3 4 5 6
n
b) iir system

clc;
clear all;
close all;
n=0:1:6
x=[1,zeros(1,6)]
b=[1 0 5]
a=[1 0.5]
y=filter(b,a,x)
subplot(2,1,1)
stem(n,x)
xlabel('n')
ylabel('x')
title('12481D3829 impulse signal')
subplot(2,1,2)
stem(n,y)
xlabel('n')
ylabel('y')
title('12481D3829 impulse response of iir system')

output waveforms:

12481D3829 impulse signal


1

0.5
x

0
0 1 2 3 4 5 6
n
12481D3829 impulse response of iir system
10

5
y

-5
0 1 2 3 4 5 6
n
Ex:3- Discrete Fourier Transform and its Properties

3(a) Discrete Fourier Transform

clc
clear all
close all
N=4
j=sqrt(-1)
xn=[1 3 -4 5]
xk=zeros(1,N)
for k=0:1:N-1
for n=0:1:N-1
xk(k+1)=xk(k+1)+xn(n+1)*exp(-j*2*pi*k*n/N)
end
end

wk=0:1:N-1
subplot(2,1,1)
stem(wk,abs(xk))
title('12481D3829 magnitude spectrum')
xlabel('k')
ylabel('abs(xk)')
subplot(2,1,2)
stem(wk,angle(xk))
title('12481D3829 phase spectrum')
xlabel('k')
ylabel('angle(xk)')
output waveforms:

12481D3829[1 3 -4 5] magnitude spectrum


15

10
abs(xk)

0
0 0.5 1 1.5 2 2.5 3
k
12481D3829 phase spectrum
2

0
angle(xk)

-2

-4
0 0.5 1 1.5 2 2.5 3
k

12481D3829 [3 -4 5 1] magnitude spectrum


15

10
abs(xk)

0
0 0.5 1 1.5 2 2.5 3
k
12481D3829 phase spectrum
2

1
angle(xk)

-1

-2
0 0.5 1 1.5 2 2.5 3
k
3(b) Discrete Fourier Transform properties

a) periodicity property

clc
clear all
close all
N=4
j=sqrt(-1)
xn=[1 3 -4 5]
n=0:1:3
xk=zeros(1,8)
subplot(2,1,1)
stem(n,xn)
title('12481D3829 original sequence')
xlabel('n')
ylabel('xn')
for k=0:1:7
for n=0:1:3
xk(k+1)=xk(k+1)+xn(n+1)*exp(-j*2*pi*k*n/N)
end
end
wk=0:1:7
subplot(2,1,2)
stem(wk,abs(xk))
title('12481D3829 magnitude spectrum')
xlabel('k')
ylabel('abs(xk)')
output waveforms:

12481D3829 original sequence


5
xn

-5
0 0.5 1 1.5 2 2.5 3
n
12481D3829 magnitude spectrum
15

10
abs(xk)

0
0 1 2 3 4 5 6 7
k
b) circular convolution property

clc
clear all
close all

x1n=input('enter i/p x1(n):')


N1=length(x1n)
x2n=input('enter i/p x2(n):')
N2=length(x2n)

N=max(N1,N2)
n=0:1:N-1
subplot(3,1,1)
stem(n,x1n)
title('12481D3829 first sequence')
xlabel('n')
ylabel('x1n')

subplot(3,1,2)
stem(n,x2n)
title('12481D3829 second sequence')
xlabel('n')
ylabel('x2n')

x1n=[x1n zeros(1,N-N1)]
x2n=[x2n zeros(1,N-N2)]
for n=0:1:N-1;
y(n+1)=0;
for l=0:N-1
p=mod(n-l,N);
y(n+1)=y(n+1)+x1n(l+1)*x2n(p+1);
end
end
disp(y)
n=0:1:3
subplot(3,1,3)
stem(n,y)
title('12481D3829 circular convolution')
xlabel('n')
ylabel('y')
x1k=zeros(1,N)
x2k=zeros(1,N)
for k=0:1:3
for n=0:1:3
x1k(k+1)=x1k(k+1)+x1n(n+1)*exp(-j*2*pi*k*n/N)
end
end
wk=0:1:N-1
figure,subplot(3,1,1)
stem(wk,abs(x1k))
title('12481D3829 magnitude spectrum x1k')
xlabel('k')
ylabel('abs(x1k)')

for k=0:1:3
for n=0:1:3
x2k(k+1)=x2k(k+1)+x2n(n+1)*exp(-j*2*pi*k*n/N)
end
end
wk=0:1:N-1
subplot(3,1,2)
stem(wk,abs(x2k))
title('12481D3829 magnitude spectrum x2k')
xlabel('k')
ylabel('abs(x2k)')

x3k=x1k.*x2k
x3n=zeros(1,N)
for n=0:1:3
for k=0:1:3
x3n(n+1)=x3n(n+1)+x3k(k+1)*exp(j*2*pi*k*n/N)/N
end
end
n=0:1:N-1
subplot(3,1,3)
stem(n,x3n)
title('12481D3829 idft')
xlabel('n')
ylabel('idft)')
output waveforms:

12481D3829 first sequence


2
x1n

0
0 0.5 1 1.5 2 2.5 3
n
12481D3829 second sequence
4
x2n

0
0 0.5 1 1.5 2 2.5 3
n
12481D3829 circular convolution
20

10
y

0
0 0.5 1 1.5 2 2.5 3
n

12481D3829 magnitude spectrum x1k


5
abs(x1k)

0
0 0.5 1 1.5 2 2.5 3
k
12481D3829 magnitude spectrum x2k
10
abs(x2k)

0
0 0.5 1 1.5 2 2.5 3
k
12481D3829 idft
20
idft)

10

0
0 0.5 1 1.5 2 2.5 3
n
Ex:4- iir filter design

a) butterworth- LPF by using bilinear and impulse invariance

clc
clear all
close all
wp=input('pass band frequency wp=')
ws=input('stop band frequency ws=')
ap=input('pass band attenuation ap=')
as=input('pass band attenuation as=')
T=1
n=(log(((10^(ap/10))-1)/((10^(as/10))-1))/(2*log(wp/ws)))
if round(n)==n

wc=wp/((10^(0.1*ap)-1))^(1/(2*n))

else n=fix(n)+1

wc=wp/((10^(0.1*ap)-1))^(1/(2*n))
end

if rem(n,2)==1
for k=1:1:(2*n)
sk(k)=(exp((j*pi*k)/(n)))
end
else
for k=1:1:(2*n)
sk(k)=(exp((j*pi)/(2*n))*exp((j*pi*k)/(n)))
end
end
pk=0
l=1
for k=1:1:(2*n)

if real(sk(k))<0

pk(l)=sk(k)
l=l+1
end
end
zk=[]
[b,a]=zp2tf(zk,pk,1)
sy=tf(b,a)
q=wc^n;
pk1=pk.*wc
[b1,a1]=zp2tf(zk,pk1,q)
sys=tf(b1,a1)
[H w]=freqs(b1,a1)
[bz,az]=bilinear(b1,a1,1)
sy=tf(bz,az,T)
[bz,az]=impinvar(b1,a1,1)
sy=tf(bz,az)
plot(w/pi,20*log10(abs(H)))
xlabel('w/pi')
ylabel('H')
title('12-3829 butterworth lpf magnitude')
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('angle')
title('12-3829 butterworth lpf phase')

output waveforms:
12-3829 butterworth lpf magnitude
0

-5

-10

-15
H

-20

-25

-30
0 50 100 150 200 250 300 350
w/pi
12-3829 butterworth lpf phase
4

1
angle

-1

-2

-3

-4
0 50 100 150 200 250 300 350
w/pi

b) chebyschev- LPF by using bilinear and impulse invariance

clc
clear all
close all
wp=input('pass band frequency wp=')
ws=input('stop band frequency ws=')
rp=input('pass band attenuation ap=')
rs=input('pass band attenuation as=')
t=(((10^(0.1*rs))-1)/((10^(0.1*rp))-1))^(0.5)
k=ws/wp
n=(acosh(t))/(acosh(k))
N=ceil(n)
e=sqrt((10^(0.1*rp))-1)
u=(1/e)+sqrt(1+1/(e^2))
a=(wp/2)*((u^(1/N))-(u^(-1/N)))
b=(wp/2)*((u^(1/N))+(u^(-1/N)))
for k=1:N
an=(pi/2)+(((2*k-1)*pi)/(2*N))
s(k)=a*cos(an)+i*b*sin(an)
end
zk=[]
[b,a]=zp2tf(zk,s,1)
sy=tf(b,a)
g1=numel(b)
g=numel(a)
if rem(N,2)==0
n=a(1,g)/sqrt(1+e^2)
else
n=a(1,g)
end
b(1,g1)=n
sy=tf(b,a)
T=1
[bz,az]=bilinear(b,a,1)
sy=tf(bz,az,T)
[H w]=freqs(b,a)
plot(w/pi,20*log10(abs(H)))
xlabel('w/pi')
ylabel('H')
title('12481D3829 chebychev lpf magnitude')
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('angle')
title('12481D3829 chebichev lpf phase')
[bz,az]=impinvar(b,a,1)
sy=tf(bz,az,T)

output waveforms:
12481D3829 chebychev lpf magnitude
0

-5

-10
H

-15

-20

-25
0 50 100 150 200 250 300 350
w/pi
12481D3829 chebichev lpf phase
0

-0.5

-1
angle

-1.5

-2

-2.5

-3
0 50 100 150 200 250 300 350
w/pi

Ex:5- fir filter design

5 a)Fir LPF
clc
clear all
close all
N=input('enter legth of the sequence')
wc=input('enter the cutoff frequency')
p=(N-1)/2
for n=0:1:N-1
h(n+1)=(wc/pi)*(sin(wc*(n-p)+0.00001))/(wc*(n-p)+0.00001)
w=rectwin(n+1)
hd(n+1)=h(n+1).*w(n+1)
b=hd
end
a=[1]
[H w]=freqz(b,a)
plot(w/pi,20*log(abs(H)))
xlabel('w/pi')
ylabel('magnitude of H(w)')
title('12481d3801 magnitude spectrum of LPF')
grid
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('phase of H(w)')
title('12481d3801 phase spectrum of LPF')
grid

output waveforms:
12481d3829 magnitude spectrum of LPF
10

-10
magnitude of H(w)

-20

-30

-40

-50

-60

-70
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi
12481d3829 phase spectrum of LPF
1.5

0.5

-0.5
phase of H(w)

-1

-1.5

-2

-2.5

-3

-3.5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi

5 b) fir hpf
clc
clear all
close all
N=input('enter legth of the sequence')
wc=input('enter the cutoff frequency')
al=(N-1)/2
for n=0:1:N-1
if (wc*(n-al))==0
h(n+1)=(sinc(pi*(n-al)))-((wc/pi)*(sinc(wc*(n-al))))
else
h(n+1)=((sin(pi*(n-al)))-(sin(wc*(n-al))))/(pi*(n-al))
end
w=rectwin(n+1)
hf(n+1)=h(n+1).*w(n+1)
b=hf
end
a=[1]
[H w]=freqz(b,a)
plot(w/pi,20*log10( abs(H)))
xlabel('w/pi')
ylabel('magnitude of H(w)')
title('12481d3801 magnitude spectrum of hPF')
grid
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('phase of H(w)')
title('12481d3801 phase spectrum of hPF')
grid
output waveforms:

12481d3829 magnitude spectrum of HPF


10

-10
magnitude of H(w)

-20

-30

-40

-50

-60

-70
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi
12481d3829 phase spectrum of HPF
4

1
phase of H(w)

-1

-2

-3

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi

5 c) fir bpf
clc
clear all
close all
N=input('enter legth of the sequence')
wc1=input('enter the lower cutoff frequency')
wc2=input('enter the upper cutoff frequency')
al=(N-1)/2
for n=0:1:N-1
if ((wc2*(n-al))==0|(wc1*(n-al))==0)
h(n+1)=(wc2/pi)*(sinc(wc2*(n-al)))-(wc1/pi)*(sinc(wc1*(n-al)))
else
h(n+1)=((sin(wc2*(n-al)))-(sin(wc1*(n-al))))/(pi*(n-al))
end
w=rectwin(n+1)
hf(n+1)=h(n+1).*w(n+1)
b=hf
end
a=[1]
[H w]=freqz(b,a)
plot(w/pi,20*log10( abs(H)))
xlabel('w/pi')
ylabel('magnitude of H(w)')
title('12481d3801 magnitude spectrum of BPF')
grid
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('phase of H(w)')
title('12481d3801 phase spectrum of BPF')
grid

output waveforms:
12481d3829 magnitude spectrum of BPF
0

-10

-20
magnitude of H(w)

-30

-40

-50

-60

-70
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi
12481d3829 phase spectrum of BPF
4

1
phase of H(w)

-1

-2

-3

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi

5 d) fir brf
clc
clear all
close all
N=input('enter legth of the sequence')
wc1=input('enter the lower cutoff frequency')
wc2=input('enter the upper cutoff frequency')
al=(N-1)/2
for n=0:1:N-1
if ((wc2*(n-al))==0|(wc1*(n-al))==0)
h(n+1)=((wc1/pi)*(sinc(wc1*(n-al))))-((wc2/pi)*(sinc(wc2*(n-al))))+(sinc(pi*(n-al)))
else
h(n+1)=((sin(wc1*(n-al)))-(sin(wc2*(n-al)))+(sin(pi*(n-al))))/(pi*(n-al))
end
w=rectwin(n+1)
hf(n+1)=h(n+1).*w(n+1)
b=hf
end
a=[1]
[H w]=freqz(b,a)
plot(w/pi,20*log(abs(H)))
xlabel('w/pi')
ylabel('magnitude of H(w)')
title('12481d3801 magnitude spectrum of BRF')
grid
figure,plot(w/pi,angle(H))
xlabel('w/pi')
ylabel('phase of H(w)')
title('12481d3801 phase spectrum of BRF')
grid

output waveforms:
12481d3829 magnitude spectrum of BRF
5

0
magnitude of H(w)

-5

-10

-15

-20
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi
12481d3829 phase spectrum of BRF
4

1
phase of H(w)

-1

-2

-3

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
w/pi

Ex:6- Applications of Adaptive Filtering

6 a)Application- unknown system identification


Clc
Clear all
Close all
INPUTVEC = 0; % Initialize a global variable
mu = .1; % Convergence factor
len = 8; % Filter length
lin =3; % Number of iterations
xin = randn(lin,1); % Input white noise
mse = 0;
x = 0*(1:len)'; % Initial input
w = 0*(1:len); % Initial filter coefs.
% Calculate first iteration
d = unsys(x(1)); % Send x(1) to the unknown sys.
y = w*x; % Calculate output of adapt. filt.
e = d-y; % Calculate error signal
mse(1) = e^2; % Calculate mean square error
w = w + mu*e*x'; % Calculate next set of coeficients
% Calculate Nth iteration
for j = 1:lin
for n = len:-1:2
x(n) = x(n-1); % Delay the Nth sample
end
x(1) = xin(j); % Get a new sample
d = unsys(x(1)); % Send the new sample to unk. sys.
y = w*x;
e = d-y;
mse(j+1) = e^2; % Calculate Nth MSE value
w = w + mu*e*x';
end
% Calculate and display the frequency spectrum of the final
% unknown system.
figure(1)
hold on
[H,f] = freqz(w,1,512,'whole');
subplot(2,1,1),plot(f,abs(H),'b')
xlabel('frequency')
ylabel('magnitude')
subplot(2,1,2),plot(f,angle(H),'b')
xlabel('frequency')
ylabel('phase')
% Calculate and plot the mean squared error for
% each iteration.
num = 0:length(mse)-1;
figure (2)
hold on
grid on
%title ('Plot of Mean Square Error vs. Iteration Number')
ylabel('MSE')
xlabel('Iteration')
plot(num,mse,'b')
% Calculate coefficient and final MSE values.
disp('Estimated coeficients w =')
disp(w')
disp('Final error e =')
disp(abs(e))

subprogram:

function dn = unsys(xn)
global INPUTVEC % Global variable
uw = [1 -1.416 5 -1.416 1]; % System Coefficients
len = length(uw); % Length of system
INPUTVEC(1) = xn; % get new sample
if length(INPUTVEC) ~= len
INPUTVEC(len) = 0;
end
dn = uw*INPUTVEC'; % convolve samples with uw
for i = len:-1:2 % update coefficients
INPUTVEC(i) = INPUTVEC(i-1);
end

output waveforms:

12-3829 Plot of frequency response


0.5

0.4
magnitude

0.3

0.2

0.1
0 1 2 3 4 5 6 7
frequency

0.4

0.2
phase

-0.2

-0.4
0 1 2 3 4 5 6 7
frequency
12-3829 Plot of Mean Square Error vs. Iteration Number
3

2.5

2
MSE

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3
Iteration

6 b)Application- noise cancellation

% Adaptive noise cancellation


% This M file recursively calculated the coeficients of
% an unknown system. The value mu specifies the rate
% of convergence of the adaptive algorithm. mu must
% be less that the smallest eigenvalue of the unknown
% transfer function for the adaptive filter to be properly
% conditioned. len is the order of the filter.
% lin is the number of iterations to take place.
% x is the input signal to the system.
% n0, n1 are two independent noise sources.
% Author: Thomas Drumright
% Date: 6/11/98
clear
mu = .01; % Convergence factor
len = 40; % Filter length
lin = 1000; % Number of iterations
n1 = 0.7*randn(lin,1); % Input white noise
n0 = n1;
mse = 0;
for n=1:lin
s(n) = sin(2*pi*(n)/abs(10*sin(pi*(1000+n)/3000)));
end
% Apply the adaptive algorithm
sn = s'+n1;
x = 0*(1:len)'; % Initial input
w = 0*(1:len); % Initial filter coefs.
% Calculate first iteration
y = w*x;
e = n0(1)-y;
mse(1) = e;
w = w + mu*e*x';
% Calculate Nth iteration
for j = 1:lin
for n = len:-1:2
x(n) = x(n-1);
end
x(1) = n0(j);
y(j) = w*x;
e = sn(j)-y(j);
mse(j) = e;
w = w + mu*e*x';
end

% Display the time domain of the input and


% the error.
f = 0:length(sn)-1;
W = fft(w,512);
figure(1)
hold on
xlabel('time')
ylabel('noisy signal')
plot(f,sn,'b')
% Calculate and plot the mean squared error for
% each iteration.
num = 0:length(mse)-1;
figure (2)
hold on
xlabel('time')
ylabel('filtered signal')
plot(num,mse,'b')
% Calculate and plot the frequency spectrums of the
% filtered and unfiltered signals.
figure (4)
hold on
t = 0:8000/(length(W)-1):8000;
plot(t,abs(W))
figure (5)
hold on
xlabel('frequency')
ylabel('noisy signal')
plot(t,abs(fft(mse,512))./512,'b')
figure(6)
hold on
xlabel('frequency')
ylabel('filtered signal')
plot(t,abs(fft(sn,512))./512,'b')
figure(7)
hold on
xlabel('time')
ylabel('MSE')
plot(f,(s-mse).^2,'b')
% Display final coeficients and MSE
disp('Estimated coeficients w =')
disp(w')
disp('Final error e =')
disp(abs(e))
snr = 10*log10(sum(abs(fft(s)).^2)/sum(abs(fft(sn-s')).^2))
snrf = 10*log10(sum(abs(fft(s)).^2)/sum(abs(fft(mse-s)).^2))
output waveforms:

1
noisy signal

-1

-2

-3
0 100 200 300 400 500 600 700 800 900 1000
time

1.5

0.5
filtered signal

-0.5

-1

-1.5

-2
0 100 200 300 400 500 600 700 800 900 1000
time
2

1.8

1.6

1.4

1.2

0.8

0 1000 2000 3000 4000 5000 6000 7000 8000

0.35

0.3

0.25
noisy signal

0.2

0.15

0.1

0.05

0
0 1000 2000 3000 4000 5000 6000 7000 8000
frequency
0.35

0.3

0.25
filtered signal

0.2

0.15

0.1

0.05

0
0 1000 2000 3000 4000 5000 6000 7000 8000
frequency

2.5

1.5
MSE

0.5

0
0 100 200 300 400 500 600 700 800 900 1000
time
CYCLE-II: IMAGE PROCESSING

7) basic operations & arithematic operations


8) removal of noise
9) image enhancement in time domain
10) image segmentation
Basic operations
clc;
clear all;
close all;
ir=imread('redrose1.jpg');
subplot(3,3,1);
imshow(ir);
title('readed image');
c2g=rgb2gray(ir);
subplot(3,3,2);
imshow(c2g);
title('grayimage');
ri=imresize(c2g,[256,256]);
subplot(3,3,3);
imshow(ri);
title('resized image');
ci1=ri(200:end,100:end);
subplot(3,3,4);
imshow(ci1);
title('crop image-1');
ci2=ri(10:120,25:255);
subplot(3,3,5);
imshow(ci2);
title('crop image-2');
fi=c2g(end:-1:1,:);
subplot(3,3,6);
imshow(fi);
title('flipped image');
subplot(3,3,7);
imhist(c2g);
title('histogram of gray image');
subplot(3,3,8);
imhist(ci1);
title('histogram of the image');
cmi=imcomplement(ri);
subplot(3,3,9);
imshow(cmi);
title('complemented image');

Output for Basic operations:

readed image grayimage resized image

flipped image
crop image-2
crop image-1

histogram of gray image histogram of the image


4000 complemented image
500
2000

0 0
0 100 200 0 100 200
arithematic operations
clc;
clear all;
close all;
i1=imread('horsebw.jpg');
i1r=imresize(i1,[256,256]);
subplot(3,3,1)
imshow(i1r)
title('resized image1')
i2=imread('manbw.jpg');
i2r=imresize(i2,[256,256]);
subplot(3,3,2)
imshow(i2r)
title('resized image2');
ai=imadd(i1r,i2r);
subplot(3,3,3);
imshow(ai);
title('added image');
si=imsubtract(i1r,i2r);
subplot(3,3,4);
imshow(si);
title('subtracted image');
mi=immultiply(i1r,i2r);
subplot(3,3,5);
imshow(mi);
title('multiplied image');
di=imdivide(i1r,i2r);
subplot(3,3,6);
imshow(di);
title('divided image');
Output for Arithematic operations:
resized image1 resized image2 added image

subtracted image multiplied image divided image

removal of noise

clc;
clear all;
close all;
ir=imread('rice.png');
subplot(1,3,1)
imshow(ir)
title('originalimage');
ni=imnoise(ir,'salt & pepper');
subplot(1,3,2)
imshow(ni)
title('noisy image');
nl=medfilt2(ni,[3 3]);
subplot(1,3,3)
imshow(nl);
title('noise less image or original image');

Output for Removal of noise :

originalimage noisy image noise less image or original image

Image enhancement in time domain


a) image eqalozation& image adjustment
clc;
clear all;
close all;
ir=imread('cameraman.tif');
subplot(3,3,1)
imshow(ir)
title('readed image');
subplot(3,3,2)
imhist(ir);
title('histogram of readed image');
e=histeq(ir)
subplot(3,3,3)
imhist(e);
title('eqalized image');
subplot(3,3,4)
imhist(e);
title('histogram of eqalized image2');
i=imread('pout.tif');
subplot(3,3,5)
imshow(i)
title('readed image');
subplot(3,3,6)
imhist(i)
title('histogram of readed image');
ai=imadjust(i,[0.3;0.5],[0.6;0.9]);
subplot(3,3,7)
imshow(ai)
title('adjusted image');
subplot(3,3,8)
imhist(ai)
title('histogram of adjusted image');
Output for image equalization & image adjustment:

histogram of readed image eqalized image


readed image
1000
1000
500
500

0 0
0 100 200 0 100 200
histogram of eqalized image2 histogram of readed image
readed image
1000
1000
500

0 0
0 100 200 0 100 200
histogram of adjusted image
adjusted image

2000

0
0 100 200
b) image smoothening & image sharpening

clc;
clear all;
close all;
ir=imread('rice.png');
subplot(1,3,1)
imshow(ir)
title('originalimage');
w=[0 1 0;1 -4 1;0 1 0]
shi=imfilter(ir,w)
subplot(1,3,2)
imshow(shi)
title('sharpening image');
ww=(1/9)*[1 1 1;1 1 1;1 1 1]
soi=imfilter(ir,ww)
subplot(1,3,3)
imshow(soi)
title('smoothening image');
Output for image smoothening & image sharpening:

originalimage sharpening image smoothening image


Image segmentation

a) line detection
clc;
clear all;
close all;
ir=imread('redrose1.jpg');
subplot(2,2,1)
imshow(ir)
title('originalimage');
w1=[-1 -1 -1;2 2 2;-1 -1 -1]
hi=imfilter(ir,w1)
subplot(2,2,2)
imshow(hi)
title('horizontal line detection');
w2=[-1 2 -1;-1 2 -1;-1 2 -1]
vi=imfilter(ir,w2)
subplot(2,2,3)
imshow(vi)
title('vertical line detection');
w3=[ 2 -1 -1;-1 2 -1;-1 -1 2]
si=imfilter(ir,w3)
subplot(2,2,4)
imshow(si)
title('slant line detection');
Output for line detection:
originalimage horizontal line detection

vertical line detection slant line detection


b)edge detection

clc;
clear all;
close all;
ir=imread('redrose1.jpg');
subplot(3,3,1)
imshow(ir)
title('originalimage');
w1=[-1 -2 -1;0 0 0;1 2 1];
hi=imfilter(ir,w1)
subplot(3,3,2)
imshow(hi)
title('sobelhorizontaledgedetection');
w2=[-1 0 1;-1 0 1;-1 0 1];
vi=imfilter(ir,w2)
subplot(3,3,3)
imshow(vi)
title('sobelverticaledgedetection');
w3=[-1 -1 -1;0 0 0;1 1 1];
phi=imfilter(ir,w3)
subplot(3,3,4)
imshow(phi)
title('prewitthorizontaledgedetection');
w4=[-1 0 1 ; -1 0 1 ; -1 0 1 ];
pvi=imfilter(ir,w4)
subplot(3,3,5)
imshow(pvi)
title('prewittverticaledgedetection');
w5=[-1 0;0 1];
rhi=imfilter(ir,w5)
subplot(3,3,6)
imshow(rhi)
title('roberthorizontaledgedetection');

Output for edge detection:

originalimage sobelhorizontaledgedetectionsobelverticaledgedetection

prewitthorizontaledgedetection
prewittverticaledgedetection
roberthorizontaledgedetection

c) point detection

clc;
clear all;
close all;
f=zeros(30,30);
f(5:5,15:15)=1;
subplot(3,3,1);
imshow(f);
title('original image');
w=[1 1 1 ;1 -8 1;1 1 1];
pi= imfilter(f,w);
subplot(3,3,2);
imshow(pi);
title('point detection');

Output for point detection:


original image point detection
morphological operations

clc;
clear all;
close all;
ir=imread('RAO.jpg');
ri=imresize(ir,[256,256]);
subplot(3,3,1)
imshow(ri)
title('original image');
se=strel('diamond',4);
di=imdilate(ri,se);
subplot(3,3,2)
imshow(di)
title('dilated image');
ei=imerode(ri,se);
subplot(3,3,3)
imshow(ei)
title('eroded image');
ni=imcomplement(ei);
subplot(3,3,4)
imshow(ni)
title('complemented image');
ai=imadd(ei,di)
subplot(3,3,5)
imshow(ai)
title('added image');
g=bwmorph(ri,'dilate')
subplot(3,3,6)
imshow(g)
title('dilation using command');
h=bwmorph(ri,'erode');
subplot(3,3,7)
imshow(h)
title('erosion using command');

Output for morphological operations:

original image dilated image eroded image

complemented image added image dilation using command

erosion using command

Potrebbero piacerti anche