You are on page 1of 7

Lab 6 Week 1 by Nolan Hergert

Contents

Pre-lab
Part 2
Part 3
Part 4
Part 5
Part 6

Pre-lab

T = 2*pi*.05;
%t = 0:T:1000*T;
t = 0:1000-1;%t(1:1000);
s = sin(T*t);

noise_orig = randn(1,1000);
noise = [zeros(1,15) noise_orig];
tau = [0 5 10 15];
figure(1);
for i = 1:4
subplot(4,1,i);
noisySignal = s+noise(16-tau(i):end-tau(i));
cleanedSignal = noisySignal - noise_orig;
plot(t,cleanedSignal);
result(i,:) = cleanedSignal;
end
Part 2

noisySignalMatrix = repmat(s,16,1);
for i = 0:15
noisySignalMatrix(i+1,:) = noisySignalMatrix(i+1,:) + noise(16-i:end-i);
end
filteredSignalMatrix = noisySignalMatrix - repmat(noise_orig,16,1);
error = filteredSignalMatrix - repmat(s,16,1);
MSE = mean((error).^2,2);
%plot(MSE);

%Error is 0 with the no-lag case. However, *any* sort of lag causes the MSE
%to jump to ~1.8 because the noise is uncorrelated with itself.

Part 3

b = fir1(31,.3,'low');
noise_lowpassed = filter(b,1,noise_orig);
figure(2);
subplot(2,1,1);
plot(t,noise_orig);
title('Original Noise');
subplot(2,1,2);
plot(t,noise_lowpassed);
title('Low-Passed Noise');

figure(3);
subplot(2,1,1);
figure(3);
subplot(2,1,1);
noisySignal = noise_lowpassed + s;
plot(t,noisySignal);
title('Noisy Signal');
subplot(2,1,2);
filteredSignal = noisySignal - noise_orig;
plot(t,filteredSignal);
title('De-Noised Signal');

figure(4);
%Low-pass noise, then delay it, then add to signal
noise = [zeros(1,30) noise_lowpassed zeros(1,30)];
noisySignalMatrix = repmat(s,61,1);
for i = 1:60
noisySignalMatrix(i+1,:) = noisySignalMatrix(i+1,:) + noise((61-i):end-i);
end
filteredSignalMatrix = noisySignalMatrix - repmat(noise_orig,61,1);
error = filteredSignalMatrix - repmat(s,61,1);
MSE = mean((error).^2,2);
plot(-30:30,MSE);
title('Effect of Lag and Low-Passed Noise on Mean-Squared Error of Naive Filter');
xlabel('Lag in samples');
ylabel('MSE');
Part 4
Part 4

%Sketch in lab handout is quite good, except it outputs FIR filter


%coefficients as an output instead of a noise-cancelled signal

Part 5

n0 = s + [zeros(1,10) noise_orig(1:end-10)];
n1 = noise_orig;
mu = .005;
h = adaptfilt.lms(32,mu);

[y,e] = filter(h,n1,n0);

%denoised = n0-y;

figure(5);
subplot(4,1,1);
plot(1:1000,s);
subplot(4,1,2);
plot(1:1000,n0);
subplot(4,1,3);
plot(1:1000,y);
subplot(4,1,4);
plot(1:1000,e);

%[s;n0;e]);
%plot(1:1000,[s;n0;e]);
%title('Adaptive Filter of Delayed Noise Signal');
%legend('Desired','Noisy Signal','Adaptfilt Output','Error');
%xlabel('time index'); ylabel('signal value');

%It's much better :)

MSE = mean((y - n0).^2,2)

MSE =

0.6191
Part 6

n0 = s + noise_lowpassed;
n1 = noise_orig;
mu = .005;
h = adaptfilt.lms(32,mu);

[y,e] = filter(h,n1,n0);

%denoised = n0-y;

figure(6);
subplot(4,1,1);
plot(1:1000,s);
subplot(4,1,2);
plot(1:1000,n0);
subplot(4,1,3);
plot(1:1000,y);
subplot(4,1,4);
plot(1:1000,e);

%figure(6);
%plot(1:1000,[s;n0;e]);
%title('Adaptive Filter of Low-Passed Noise Signal');
%legend('Desired','Noisy Signal','Adaptfilt Output','Error');
%xlabel('time index'); ylabel('signal value');

MSE = mean((y - n0).^2,2)


MSE = mean((y - n0).^2,2)

%Much better results here as well

MSE =

0.5368

Published with MATLAB® 7.10

You might also like