Friday, July 17, 2009

Chapter 2

% Program 2_1
% Generation of the ensemble average
%
R = 50;
m = 0:R-1;
s = 2*m.*(0.9.^m); % Generate the uncorrupted signal
d = rand(R,1)-0.5; % Generate the random noise
x1 = s+d';
stem(m,d);
xlabel('Time index n');ylabel('Amplitude'); title('Noise');
pause
for n = 1:50;
d = rand(R,1)-0.5;
x = s + d';
x1 = x1 + x;
end
x1 = x1/50;
stem(m,x1);
xlabel('Time index n');ylabel('Amplitude'); title('Ensemble average');

% Program 2_2
% Generation of complex exponential sequence
%
a = input('Type in real exponent = ');
b = input('Type in imaginary exponent = ');
c = a + b*i;
K = input('Type in the gain constant = ');
N = input ('Type in length of sequence = ');
n = 1:N;
x = K*exp(c*n);%Generate the sequence
stem(n,real(x));%Plot the real part
xlabel('Time index n');ylabel('Amplitude');
title('Real part');
disp('PRESS RETURN for imaginary part');
pause
stem(n,imag(x));%Plot the imaginary part
xlabel('Time index n');ylabel('Amplitude');
title('Imaginary part');

% Program 2_3
% Generation of real exponential sequence
%
a = input('Type in argument = ');
K = input('Type in the gain constant = ');
N = input ('Type in length of sequence = ');
n = 0:N;
x = K*a.^n;
stem(n,x);
xlabel('Time index n');ylabel('Amplitude');
title(['\alpha = ',num2str(a)]);

% Program 2_4
% Signal Smoothing by a Moving-Average Filter
%
R = 50;
d = rand(R,1)-0.5;
m = 0:1:R-1;
s = 2*m.*(0.9.^m);
x = s + d';
plot(m,d,'r-',m,s,'b--',m,x,'g:')
xlabel('Time index n'); ylabel('Amplitude')
legend('d[n]','s[n]','x[n]');
pause
M = input('Number of input samples = ');
b = ones(M,1)/M;
y = filter(b,1,x);
plot(m,s,'r-',m,y,'b--')
legend('s[n]','y[n]');
xlabel ('Time index n');ylabel('Amplitude')

% Program 2_5
% Illustration of Median Filtering
%
N = input('Median Filter Length = ');
R = 50; a = rand(1,R)-0.4;
b = round(a); % Generate impulse noise
m = 0:R-1;
s = 2*m.*(0.9.^m); % Generate signal
x = s + b; % Impulse noise corrupted signal
y = medfilt1(x,3); % Median filtering
subplot(2,1,1)
stem(m,x);axis([0 50 -1 8]);
xlabel('n');ylabel('Amplitude');
title('Impulse Noise Corrupted Signal');
subplot(2,1,2)
stem(m,y);
xlabel('n');ylabel('Amplitude');
title('Output of Median Filter');

% Program 2_6
% Illustration of Convolution
%
a = input('Type in the first sequence = ');
b = input('Type in the second sequence = ');
c = conv(a, b);
M = length(c)-1;
n = 0:1:M;
disp('output sequence =');disp(c)
stem(n,c)
xlabel('Time index n'); ylabel('Amplitude');

% Program 2_7
% Computation of Cross-correlation Sequence
%
x = input('Type in the reference sequence = ');
y = input('Type in the second sequence = ');
% Compute the correlation sequence
n1 = length(y)-1; n2 = length(x)-1;
r = conv(x,fliplr(y));
k = (-n1):n2';
stem(k,r);
xlabel('Lag index'); ylabel('Amplitude');
v = axis;
axis([-n1 n2 v(3:end)]);

% Program 2_8
% Computation of Autocorrelation of a
% Noise Corrupted Sinusoidal Sequence
%
N = 96;
n = 1:N;
x = cos(pi*0.25*n); % Generate the sinusoidal sequence
d = rand(1,N) - 0.5; % Generate the noise sequence
y = x + d; % Generate the noise-corrupted sinusoidal sequence
r = conv(y, fliplr(y)); % Compute the correlation sequence
k = -28:28;
stem(k, r(68:124));
xlabel('Lag index'); ylabel('Amplitude');

No comments: