MATLAB Question: Running average filter is a way for smoothing noisy data set. F
ID: 3726101 • Letter: M
Question
MATLAB Question: Running average filter is a way for smoothing noisy data set. For each data sample in a running average filter, the program examines a subset of n samples centered on the sample under test, and it replaces that sample with the average value from the n samples. (Note 1: For points near the beginning and the end of the data set, use a small number of samples is the running average, but be sure to keep equal number of samples on either side of the sample under test; Note 2: Be sure to create another variable to hold the smoothed data to avoid changing the original data during the smoothing process.) Write a MATLAB function to perform a running average filter on a data set. Write a program that allows the user to specify the name of an input dataset and the number of samples n to average in the filter and then call the MATLAB function to perform the running average filter on the data. Use the load function to load the data. Use the input function with ‘s’ option to get the file name and n from the user. Use the data in the “input.txt” file for testing your program. The program should plot both the original data the smoothed curve after the running average filter.
Explanation / Answer
Matlab code:
function outSig = RunningAverageFilter(inp, n)
%File Name: RunningAverageFilter.m
outSig = zeros(1, length(inp));
%Define output signal
%Find n is even or odd
tp = mod(n, 2);
len = n;
if tp==0
len = len +1;
end
padlen = (len-1)/2;
%Define pad len
padZeros = zeros(1, padlen);
%Define pad zeros
newInp = [padZeros inp padZeros];
%Create new input after padding zeros
for kk =1: length(inp)
%Loop to perform average filtering
sumtp = 0;
%Define value
%Loop to perform the sum
for aa =1 : n
%Find sum
sumtp += newInp(kk+aa-1);
%End
end
%Find output
outSig(kk) = sumtp / n;
end
%Function end
end
%File Name: MyScript.m
%Get file name
fileName = input('Enter the file name:', 's');
%Get n value
n = input('Enter n value:');
%Open the file
fID = fopen(fileName, 'r');
%Read the input
A = fscanf(fID, '%d ');
%Close the file
fclose(fID);
%Set input
x = A';
%Call Function
out = RunningAverageFilter(x, n);
%Define value for x axis
tp = 1:length(x);
%Hold on
hold on
%Plot original data
plot(tp, x, '--*')
%Plot smoothed curve
plot(tp, out, '--o')
legend('Original data', 'Smoothed Curve');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.