Write a MATLAB program that constructs a sampled data waveform, filters it using
ID: 673168 • Letter: W
Question
Write a MATLAB program that constructs a sampled data waveform, filters it using a simple digital filter, and displays the resulting input and output waveforms. The output of the program should include plots of the input and output waveforms x[n] and y[n]. In order to facilitate direct comparisons, x[n] and y[n] will need to be plotted one above the other using a common axis for the sample time. Users should be able to specify the use of either an averaging filter with a user-specified number m of input samples, or an exponential filter with a user-specified filter constant alpha. The equations of the two types of filter are as follows: Averaging filter: y[n] = (x[n] + x[n - 1] + x[n - 2] ... + x[n - m + 1 ])/m Exponential filter: y[n] = ay[n - 1] + (1 - alpha)x[n], with a between 0 and 1.Explanation / Answer
clc;
clear all;
close all;
function y = gen_move_aver (x)
% GEN_MOVE_AVER
% Filter an input sequence x by a moving average filter
% whose coefficients are in b.
% Undefined values of y (from k = 1 to M-1) are left as zero.
N = length(x);
y = zeros (1,N);
%this create a vector with N zeros
for k = 1:N
for j = 1:N
y(k) = [y(k) + x(k-j+1)];
end
end
y(k)=y(k)/N;
plot(t,x)
hold on
plot(t,y)
grid on
legend('Input Data','Filtered Data','Location','NorthWest');
title('Plot of Input and Filtered Data');
clc;
close all;
clear all;
function y = exponential_filter (a,n,p)
% p is the alpha value
% n number of inputs
N = length(n);
y = zeros (1,N);
%this create a vector with N zeros
for k = 1:N
y(k) = [y(k) + a*y(N-1)+(1-p)x(N)];
end
end
plot(t,x)
hold on
plot(t,y)
grid on
legend('Input Data','Filtered Data','Location','NorthWest')
title('Plot of Input and Filtered Data')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.