0% found this document useful (0 votes)
9 views

Carpio_ECE104.1_Laboratory 8 Implementation of Digital Filter in Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Carpio_ECE104.1_Laboratory 8 Implementation of Digital Filter in Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Republic of the Philippines

Mindanao State University -


Iligan Institute of Technology
COLLEGE OF ENGINEERING AND TECHNOLOGY
Department of Electrical Engineering and Technology

LAB 7: Digital Filter Design


ECE104.1

Submitted by:
JARED KENAN P. CARPIO
V - BS ECE

Submitted to:
ENGR. PROF. OLGA JOY L. GERASTA
ECE104.1 Professor
Implementation of Digital Filter in Matlab

Laboratory 8

In statistics, a moving average (rolling average or running average or moving mean


or rolling mean) is a calculation to analyze data points by creating a series of averages of
different selections of the full data set. Variations include: simple, cumulative,
or weighted forms.

Mathematically, a moving average is a type of convolution. Thus in signal


processing it is viewed as a low-pass finite impulse response filter. Given a series of
numbers and a fixed subset size, the first element of the moving average is obtained by taking
the average of the initial fixed subset of the number series. Then the subset is modified by
"shifting forward"; that is, excluding the first number of the series and including the next
value in the subset.

A moving average is commonly used with time series data to smooth out short-term
fluctuations and highlight longer-term trends or cycles. The threshold between short-term
and long-term depends on the application, and the parameters of the moving average will be
set accordingly. It is also used in economics to examine gross domestic product,
employment or other macroeconomic time series. When used with non-time series data, a
moving average filters higher frequency components without any specific connection to
time, although typically some kind of ordering is implied. Viewed simplistically it can be
regarded as smoothing the data.

On the other hand, alternative to the moving-average filter is the median filter. Where
a moving average filter takes the arithmetic mean of the input over a moving sample window,
a median filter (per the name) takes a median instead.

The median filter is most useful for removing occasional outliers from an input
stream. This makes it particularly well-suited to filtering inputs from distance sensors, which
are prone to occasional interference. Unlike a moving average, the median filter will remain
completely unaffected by small numbers of outliers, no matter how extreme.

Also, FIR filters are widely used because of the powerful design algorithms that exist to
design such filters. These filters are also inherently stable when implemented in the
nonrecursive form, and you can easily attain linear phase and also extend them to multirate
cases, and there is ample hardware support for these filters.
I. Script: Moving Average and Moving Median Filter

Sample Design

-----------------------------------------------

clc

clear all

n=0:0.001:10;

y=10*sin(2*pi*2*n);

noisy_y=y+[10*rand(1,length(n))];

%noisy_y=y+(10*sin(2*pi*20*n));

subplot(311);

plot(n,y);

title('The original signal');

subplot(312);

plot(n,noisy_y,'r');

title('Noisy signal');

window=100;

filtered_y=movmean(noisy_y,window);

%filtered_y=movmedian(noisy_y,window);

subplot(313);

plot(n,filtered_y,'g');

title('Filtered signal');

---------------------------------

Note: Replace the filtered script line by adding/removing “ % “ to alternate filtered script
Edited code and Results

noisy_y=y+[10*rand(1,length(n))]; & filtered_y=movmedian(noisy_y,window);

noisy_y=y+[10*rand(1,length(n))]; & filtered_y=movmean(noisy_y,window);

This section explores the filtered signal by applying either the moving average and
moving median as filters to the noise. The moving average filter smoothed out the noise by
calculating the mean across a specified period showing its effectiveness for general noise
suppression. The moving median filter excels in handling outliers, showing better
performance when dealing with extreme deviations in the signals.
noisy_y=y+(10*sin(2*pi*20*n)); & filtered_y=movmedian(noisy_y,window);

noisy_y=y+(10*sin(2*pi*20*n)); & filtered_y=movmean(noisy_y,window);

We tested two scenarios of noises via random function and sinusoidal function. The
random function (+10*rand) highlights the smoothing capabilites of both filters as it
performed well in reducing overall variability while the moving median retained signal shape
better against sudden spikes. The sinsusoidal function (+10*sin) introduced periodic
distortion in which the moving mean provided a better noise surpression than the moving
median.
Using MovMedian

Using MovMean
II. Fir low pass filter

Sample Design cut_off=3e3/(Fs/2);

Fs=200e3; % sampling frequency order=64;

Ts=1/Fs; % sampling period or time step h=fir1(order,cut_off);

n=0:Ts:(5e-3)-Ts %signal duration subplot(323);

f1=2e3; stem(h);

f2=7e3; title('Filter impulse response');

f3=30e3; subplot(324);

y1=10*sin(2*pi*f1*n); f_h=freqz(h);

y2=10*sin(2*pi*f2*n); plot(abs(f_h));

y3=10*sin(2*pi*f3*n); title('Filter frequency response');

y=y1+y2+y3; filter_out=conv(y,h);

subplot(321); subplot(325);

plot(n,y,'r'); plot(filter_out,'g');

title('Noisy Signal') title('Filtered signal');

ny=length(y); f_y=length(filter_out);

ny_fft=2^nextpow2(ny); f_fft=2^nextpow2(f_y);

yfft=fft(y,ny_fft); filter_fft=fft(filter_out,f_fft);

yfft2=yfft(1:ny_fft/2); filter_fft2=filter_fft(1:f_fft/2);

xfft=Fs*(0:ny_fft/2-1)/ny_fft; ff_fft=Fs*(0:f_fft/2-1)/f_fft;

subplot(322); subplot(326);

plot(xfft,abs(yfft2),'r'); plot(ff_fft,abs(filter_fft2),'g');

title('Noisy signal in frequency domain'); title('Filtered signal in frequency domain');

Note: Understand the script. Replace the Fs(sampling frequency domain with the last digit
of your ID number.
Edited code and Results

Fs = 0581
In this section, we design a filter using the fir1 function and we used this a FIR low-
pass filter to attenuate high-frequency components in a multi-frequency signal. The output
demonstrates significant attenuation of higher frequencies whilst preserving the lower-
frequency components. The frequency domain analysis shows the filter’s effectiveness in
elimination unwanted spectral content beyond the cutoff frequency and its stability and
linear-phase characteristics is highlighted by the impulse and frequency response of the
design of the filter.
Conclusion

In this laboratory activity, we get to play around the signals and also the digital filters
using movmean and movmedian. We can see the important role of digital filters in signal
processing and their adaptability to various noise and distortion types. In the first part of the
laboratory, we were able to compare the movmean and movmedian filters. The moving
average filter efficiently smoothens random noise but is less effective against outliers and
periodic distortions while the moving median filter is better against extreme deviations or
oscillatory noise, in fact it removes them. The moving median provides a more robust
estimate of the trend compared to moving average. In the second part, we were able to code
with the FIR filter and it effectively separated desired signal components from noise in both
time and frequency domains.

Using MATLAB really does help in providing the proper simulation outputs when
designing and analyzing digital filters. It bridges theoretical knowledge and practical
implementation by being able to explore different filters and their behaviors, this laboratory
exercise has helped me further my understanding of the importance of selecting appropriate
filtering methods based on the signal characteristics and the processing goal.

You might also like