Download - Final Edited Dsp
1.0 Title
Digital Filtering of Audio Signals
2.0 Objectives
To identify and analyze a digital audio signal with added noise.
To identify these unknown noise signal (filter specification) and to eliminate them using
FIR as well as IIR digital filters (determining filter order and coefficient).
To apply the knowledge that you obtained from the lecture to accomplish the task by
using MATLAB
3.0 Result
3.1 Part A: Comparison between Original and Noise Signal in Time Domain and Frequency Domain1. Matlab Coding
Line Command Comment1 [ori, fs]= wavread('D:\Benw\sem7\DSP\
wave_files\G2o.wav');% get frequency sampling from original sound
2 wavplay(ori,fs); % play the original sound from wavread3 fs; % define the frequency sampling4 a0=0:1/fs:(length(ori)-1)/fs; %takes depends on the original sound and the
number of stages in the filter5 figure (1); %create figure graphic object6 subplot(2,2,1); plot(a0,ori); %define the subplot location (2,2,1) & plot the
a0 and original signal7 title('Original signal in time domain'); %title of the subplot(2,2,1)8 xlabel('Time (s)'); %name of the label x as Time in second9 ylabel('Amplitude'); %name of the label y as Amplitude10 grid; %put the grid on the graph11 m=length(ori)-1; %define the function n which is length of
original song -1.12 t1=0:fs/m:fs; %define the grid range depends on sampling size13 wavefft=abs(fft(ori)); % fast fourier transform signal is equal
absolute value14 figure(1); %create figure graphic object15 subplot(2,2,3); plot(t1,wavefft); %define the subplot location (2,2,3)& plot the
t1 and fft signal16 title('Original signal in frequency domain'); %title of the plotted graph17 xlabel('Frequency (Hz)') %x axis name as Frequency18 ylabel('Magnitude'); %y axis name as Magnitude19 grid; %enable grid20 [noise, ft] = wavread('D:\Benw\sem7\DSP\
wave_files\G2n.wav');% to get frequency sampling from noise sound
21 wavplay(noise,ft); % play the noise sound from wavread22 ft; % determine the frequency sampling23 a0=0:1/ft:(length(noise)-1)/ft; %takes depends on the noise sound to be
filtered and the number of stages in the filter24 figure (1); %create figure graphic object25 subplot(2,2,2); plot(a0,noise); %define the subplot location (2,2,2)& plot the
a0 and noise signal26 title('Noisy signal in time domain'); %title of the plotted graph
27 xlabel('Time (s)'); %x axis name as Time28 ylabel('Amplitude'); %y axis name as Magnitude29 grid; %enable grid30 n=length(noise)-1; %define the function n which is length of noise
song -1.31 f2=0:ft/n:ft; %define the grid range depends on sampling size32 wavefft2=abs(fft(noise)); % fast fourier transform signal is equal
absolute value33 figure(1); %create figure graphic object34 subplot(2,2,4); plot(f2,wavefft2) %define the subplot location (2,2,4)& plot the
f2 and fft signal35 title('Noisy signal in frequency domain'); %title of the plotted graph36 xlabel('Frequency (Hz)'); %x axis name as Frequency(Hz)37 ylabel('Magnitude'); %y axis name as Magnitude38 grid; % allow the grid view
Figure 3.1.1: Comparison between Original Signal and Noise Signal in Time Domain and Frequency Domain.
Figure 3.1.2:Value of Sampling Frequency,ft.
Figure 3.1.3: The ways to find the Passband Frequency,fp and the Stopband Frequency, fs.
1. As the result in Matlab showed the frequency sampling,f t for both signal which are original and noise sound is 11025Hz. As in the Figure 3 below, we can see that the noise happen when 2240Hz, thus the chosen for passband frequency, fp is 2140Hz and the stopband frequency, fs is 2240Hz. So,
ωP=2πf Pf t
=2π (2140 )11025
=0 .388 π ωS=2πf Sf t
=2π (2240)11025
=0 . 406 π
The cut-off frequency, fc is 2190Hz. Asf C=( f P+ f S )/2=2190Hz . So,
ωC=(ωP+ωS )
2=
(0 .388+0 .406)2
=0.397
3.2 Part B: FIR Filter Design and Filtering
1. Find the Order for FIRThe requirement of the filter which the peak passband ripple is 3 dB and minimum stopband attenuation ois30 dB. Thus, the Hanning Window is more suitable choosing to design the filter, which in theoritical stopband attenuation for hanning window is αs ≤ 44dB. For the formula of Hanning are shows as below:
N=3 . 1Δf
= 3 .1( f S−f P)/ f t
= 3 .1(2240−2140 )/11025
=341order
Line Command Comment1 [noise,freq]=wavread('D:\Benw\sem7\DSP\
wave_files\G2n.wav');% play the noise sound
2 a=fir1(341,0.397,hanning(342)); % determine order = 341, cut-off frequency = 0.397
3 figure (4); %create figure graphic object4 subplot(3,2,1); %define the subplot location5 stem (a); % plot the discrete data of filter hanning
window6 grid; % allow the grid view
7 title('Impulse Response Coefficient'); %title of the plotted graph8 xlabel ('Time Index n'); %name of the label x as Time Index n9 ylabel('Amplitude'); %name of the label y as Amplitude10 figure (4); %create figure graphic object11 subplot (3,2,3); %define the subplot location12 [h,w] = freqz(a,1,512); %digital frequency response(numerator and
denominator)13 plot (w/pi, 20*log10 (abs(h))); % plotted to define range window14 grid; % allow the grid view15 xlabel ('\omega/\pi'); %name of the label x16 ylabel ('Gain, dB'); %name of the label y17 title ('Lowpass filter designed using Hann
Window');%title of the plotted graph
18 axis ([0 1 -80 10]); %range of axis for Hanning window plot19 y = filter(a,1,noise);
20 wavwrite(y,'fir1.wav'); % to write the data stored21 [signal,freq]=wavread('fir1.wav'); % to get frequency sampling from noise22 n = length(signal); % define length of signal23 sound (signal,freq); % play the noise filter file ‘fir1.wav’
from wavread24 figure (4); %create figure graphic object25 subplot (3,2,2); %define the subplot location26 plot((1:n)/freq,signal); % plotted filter signal in time domain27 title('Filtered signal in time domain'); %title of the plotted graph28 xlabel('Time (s)'); %name of the label x29 ylabel('Amplitude'); %name of the label y30 grid; % allow the grid view31 figure(4); %create figure graphic object32 signal0=signal-mean(signal); % the value of signal1033 fsignal=fft(signal0); % the axis value of fsignal34 subplot (3,2,4); %define the subplot location35 plot((1:n/2)/n*freq,abs(fsignal(1:n/2))); % plotted filter signal in freq domain36 grid; % allow the grid view37 title('Filtered signal in frequency domain'); %title of the plotted graph38 xlabel('Frequency (Hz)'); %name of the label x39 ylabel('Magnitude'); %name of the label y40 figure(4); %create figure graphic object41 subplot (3,2,[5 6]); %define the subplot location42 zplane(a, 1) % plot zero pole at z-plane43 title('Z-plane'); %title of the plotted graph44 grid; % allow the grid view
Table 1: Matlab Coding (design filter response coding)
Figure 3.2.1: Plot the designed filter response 341 order
Line Command Comment1 [ori, fs] = wavread('D:\Benw\sem7\DSP\
wave_files\G2o.wav'% get frequency sampling from original sound
2 wavplay(ori,fs); % play the original sound from wavread3 fs; % define the frequency sampling4 a0=0:1/fs:(length(ori)-1)/fs; %takes depends on the original sound and
the number of stages in the filter5 figure (1); %create figure graphic object6 subplot(3,2,1); plot(a0,ori); %title of the plotted graph7 title('Original signal in time domain'); %title of the subplot(2,2,1)8 xlabel('Time (s)'); %name of the label x as Time in second9 ylabel('Amplitude'); %name of the label y as Amplitude10 grid; %put the grid on the graph11 m=length(ori)-1; %define the function n which is length of
original song -1.12 t1=0:fs/m:fs;
13 wavefft=abs(fft(ori)); % fast fourier transform signal is equal absolute value
14 figure(1); %create figure graphic object15 subplot(3,2,2); plot(t1,wavefft) %define the subplot location (3,2,2)& plot
the t1 and fft signal16 title('Original signal in frequency domain'); %title of the plotted graph17 xlabel('Frequency (Hz)') %name of the label x18 ylabel('Magnitude'); %name of the label y19 grid; % allow the grid view20 [noise, ft] = ('D:\Benw\sem7\DSP\wave_files\
G2n.wav');% to get frequency sampling from noise sound
21 wavplay(noise,ft); % play the noise sound from wavread22 ft; % determine the frequency sampling23 a0=0:1/ft:(length(noise)-1)/ft; %takes depends on the noise sound to be
filtered and the number of stages in the
filter24 figure (1); %create figure graphic object25 subplot(3,2,3); plot(a0,noise); %define the subplot location (3,2,3)& plot
the a0 and noise signal26 title('Noisy signal in time domain'); %title of the plotted graph27 xlabel('Time (s)'); %name of the label x as Time in second28 ylabel('Amplitude'); %name of the label y as Amplitude29 grid; % allow the grid view30 n=length(noise)-1; % length of the signal31 f2=0:ft/n:ft; % range of frequency f232 wavefft2=abs(fft(noise)); % fast fourier transform signal is equal
absolute value33 figure(1); %create figure graphic object34 subplot(3,2,4); plot(f2,wavefft2) %define the subplot location (3,2,4)& plot
the f2 and fft signal35 title('Noisy signal in frequency domain'); %title of the plotted graph36 xlabel('Frequency (Hz)') %name of the label x as Frequency in Hz37 ylabel('Magnitude'); %name of the label y as Magnitude38 grid; % allow the grid view39 n = length(signal); % define length of signal40 sound (signal,freq); % play the noise filter file ‘fir1.wav’
from wavread41 figure (1); %create figure graphic object42 subplot (3,2,5); %define the subplot location43 plot((1:n)/freq,signal); % plotted filter signal in time domain44 title('Filtered signal in time domain'); %title of the plotted graph45 xlabel('Time (s)'); %name of the label x as Time in second46 ylabel('Amplitude'); %name of the label y as Amplitude47 grid; % allow the grid view48 figure(1); %create figure graphic object49 signal0=signal-mean(signal); % the value of signal1050 fsignal=fft(signal0); % the axis value of fsignal51 subplot (3,2,6); %define the subplot location52 plot((1:n/2)/n*freq,abs(fsignal(1:n/2))); %delay the truncated h[n] by n/2 samples53 grid; % allow the grid view54 title('Filtered signal in frequency domain'); %title of the plotted graph55 xlabel('Frequency (Hz)'); %name of the label x as Frequency in Hz56 ylabel('Magnitude'); %name of the label y as Magnitude
Table 2: Comparison between Original Signal, Noise Signal and Filtered Signal
Figure 3.2.2:Comparison FIR design before and after filter noise.
Part C: IIR Filter Design and Filtering
1. Find the Order for IIRThe requirement of the filter which require the peak passband ripple is 3 dB and minimum stopband attenuation is 30 dB. Thus, the Buterworth Window is more suitable choosing to design the filter.
d=[(1−δP)−2−1
δS−2−1 ]
1/2
=[(0 .708 )−2−1
(0 .032 )−2−1 ]1/2
=[0 . 995975 .563 ]
1/2
=0 . 032
αP=|20 log10(1−δP)|dB ,αP=−3dB→(1−δP)=0 . 708α S=|20 log10(δS )|dB ,α S=−30dB→(δS )=0. 032
k=ΩP
ΩS=
2T (0 . 388π
2 )2T (0 . 406 π
2 )=
0. 3880. 406
=0 . 956
∴N≥log dlog k
≥log 0 .032log 0 .956
N≥70 order
Line Command Comment
1 [noise,freq]=wavread('D:\Benw\sem7\DSP\wave_files\G2n.wav'
% play the noise sound
2 [b,a]= butter(60,0.397); % determine order= 10, cut-off frequency= 0.48
3 [h, omega] =freqz(b,a,512); % digital frequency response(numerator and denominator)
4 y = filter(b,a,noise); % the parameters for filter y5 wavwrite(y,'fir1IIR.wav'); % to write the data stored
The order of the
6 [signal,freq]=wavread('fir1IIR.wav'); % to get frequency sampling from noise sound
7 n = length(signal); % define length of signal8 sound (signal,freq); % x and y of signal sound9 figure(6); %create figure graphic object10 subplot(3,2,1); %define the subplot location11 plot(omega/pi, 20*log(abs(h))); % plotted filter signal in time domain12 grid; % allow the grid view13 xlabel('\omega/\pi');ylabel('Gain,in dB'); %name of the label x14 ylabel('Gain,in dB'); %name of the label y15 title('Type Butter Filter'); %title of the plotted graph16 axis([0 1 -60 5]); % the scale of axis x and y17 figure(6); %create figure graphic object18 subplot(3,2,3); %define the subplot location19 plot(omega/pi, unwrap(angle(h))); % to produce smoother phase plots20 grid; % allow the grid view21 axis([0 1 -8 1]); % the scale of axis x and y22 xlabel('\omega/\pi'); %name of the label x23 ylabel('Phase,radians'); %name of the label y24 title('Type Butter Filter'); %title of the plotted graph25 figure (6); %create figure graphic object26 subplot (3,2,2); %define the subplot location27 plot((1:n)/freq,signal); % the scale of axis x and y28 title('Filtered signal in time domain'); %title of the plotted graph29 xlabel('Time(s)'); %name of the label x30 ylabel('Amplitude'); %name of the label y31 grid; % allow the grid view32 figure(6); %create figure graphic object33 signal0=signal-mean(signal); % the value of signal1034 fsignal=fft(signal0); % the axis value of fsignal35 subplot (3,2,4); %define the subplot location36 plot((1:n/2)/n*freq,abs(fsignal(1:n/2))); % plotted filter signal in freq domain37 grid; % allow the grid view38 title('Filtered signal in frequency domain'); %title of the plotted graph39 xlabel('Frequency (Hz)'); %name of the label x40 ylabel('Magnitude'); %name of the label y41 figure(6) %create figure graphic object42 subplot (3,2,[5 6]); %define the subplot location43 zplane(b, a); % plot zero pole at z-plane44 title('Z-plane'); %title of the plotted graph45 grid; % allow the grid view
Table 3: Matlab Coding
Figure 3.2.3: The Butterworth Designed Low Pass Filter 70th order Response
2.
Figure 3.2.4: Comparison IIR Design before and after filter noise.
4.0 DISCUSSION
i. Plotting Audio Signal without noise and Audio Signal with Noise in Time and Frequency Domain
Based on the filter knowledge, the MATLAB present a few built-in functions that allocate one to
import and export audio files. It is understood that audio files sustained to be use in Matlab is Microsoft
WAV ('pronounced 'wave') format. What we mean by built-in functions is, it is can be read and written
to/from Matlab using built-in 'wavread' and wavwrite' functions.
To create the given audio signal in time and frequency domain, the command ‘wavread’ is used
followed by the location of the audio files, in bracket. If this line of command was skipped, there is no
audio file will be retrieved. It is noted that the filenames and any text messages must be single quoted in
Matlab. The command 'wavplay' is also require to be used in order to play the audio file. The frequency
sampling from audio signal is obtained by using command 'fs'.
The command [ori, fs] = wavread('audio file location') at line 1 will build the array ori contains
the sound data and fs is the sampling frequency. The data is sampled at the same rate as that on a music
CD (fs=44,100 samples/second). We say like this because, analog audio is recorded by sampling it 44,100
times per second and then these samples are used to recreate the audio signal when playing it back.
To compute audio spectra in Matlab, the function 'fft' is used to computes FFT of that two audio
signals. The magnitude/phase values of FFT coefficient was determined by using command 'abs' (line 13).
The length of the plotted audio signal is written as t1=0:1/fs:(length(ori)-1)fs.
From the result in Matlab showed the frequency sampling,f t for both original and noise sound is
11025Hz. As in the Figure 3, the passband and stopband edge frequency are identified by zooming the
output signal which shows that fs=2240Hz and fp=2140Hz. Low pass filter and high pass filter were
actually the symmetry of signal. Only half of the filter was taken because it is symmetry so low pass filter
is chosen. Based on the fs and fp, the cut off frequency, ώc can be calculated.
ii. FIR Filter Design
In this task, the window technique is used for FIR filter design. The filter order and its coefficient
are first determined. Given, peak passband ripple of 3 dB and minimum stopband attenuation of 30 dB.
So, Hanning Window was choosen. The FIR order was 341 because according “a = fir(341,0.397,hanning
(342))” command, which number of order is calculated due to N = 3.1/DF for Hanning window. While
“hanning(342)” shows a causal FIR transfer function H(z) of length N+1 → 341+1= 342 is a polynomial
in z-1 of degree N. While the 0.397=cut off frequency.
Then, the function of “[h,w] = freqz(b,1,512) “ command is to use the transfer function
associated with the multirate filter to calculate the frequency response of the filter with the current
coefficient values. The “wavwrite(y,Fs,N,filename)” command used to write the data stored in the
variable y to a WAVE file called filename.
The command 'wavwrite(y,'fir1.wav'); is to save an array as a .wav audio file. This audio file is
the new audio file where the noises have been filtered. It is automatically inside matlab folder path with
name fir1.wav. It depends on user, if they want to named it as 'mysong' so it should be written as :
wavwrite(y,'mysong.wav'). To get frequency sampling from noise sound, command
[signal,freq]=wavread('fir1.wav') was used.
The “plot ((1:n/2)/n*freq, abs(fsignal(1:n/2)))” command especially n/2 used because h[n] is
finite for positive and negative time for ideal filter is non-causal. To overcome the problem the solution is
by delaying the truncated h[n] by n/2 samples.
After FIR filter been applied (Figure 4), due to the z-plane in FIR filter design there are zeroes
include in unit circle. In MATLAB zplane(b, 1) command representing where b are the coefficients
obtained from fir1(),while a=1 since this is an FIR filter. The results based on Figure 4 showed the
filtered signal graph, noise signal and original signal in the form of time domain and frequency domain.
The signal obtained after filtering process by using Hanning window approximately similar with the
original signal in time domain and frequency domain. However, in frequency domain the filtered signal
sample has half of the sample compared to original signal’s sample. This is because the resulting impulse
response, h[n-M/2] is causal, stable FIR filter.
Based on comparison (Figure 5), there are obvious differences between audio file without noise
and with noise, in time and frequency domain. In time domain, audio signal with noise looked almost
periodic over short time interval compare to the audio signal without noise, which shows that original
audio signal has very high frequency due to the presence of noise.
While in frequency domain, the noises are clearly can be identified compared to time domain. In
frequency domain, it shows that low frequency signal is being interrupted by high frequencies
approximately at 2240Hz and the signal is symmetric. Therefore, the elimination of noise is implemented
in frequency domain by removing high frequencies (noises that attached to original audio signal) to
retrieve back the original audio signal.
After the FIR filter been applied, a high frequency components have been successfully
suppressed in the output, which satisfied our expectation and theory. The audio files seems to reduce its
noisy sound when we playing the audio file.
iii. IIR Filter Design
In this IIR filter design, given peak passband ripple of 3 dB and minimum stopband attenuation
of 30 dB. So, Butterworth Window was choosen because Butterworth refers to a type of filter response. It
is sometimes called the Maximally Flat approximation, because for a response of order n, the first
derivatives of the gain with respect to frequency are zero at frequency = 0. There is no ripple in the low
pass band, and DC gain is maximally flat. This command representing [h, omega] =freqz(b,a,512), while
freqz is used to declare b and a which contain numerator and denominator. The z-plane for IIR design
filter showed that the poles and zeroes included in unit circle (Figure 6).
In this IIR filter, the order,N is 60. The function of MATLAB [b,a]= butter(70,0.397) designs an
Nth order=60 low pass digital Butterworth filter and returns the filter coefficient in length N+1 vectors
b=numerator and a=denominator. The cut off frequency wn = Y.YY because wn must be 0.0< wn<1.0 with
1.0 corresponding to half the sample rate. The signal obtained after filtering process by using Butterworth
filter is approximately similar compared to the original signal in the form of time domain and frequency
domain.
Based on comparison (Figure 7), there are obvious differences between audio file without noise
and with noise, in time and frequency domain. In time domain, audio signal with noise looked almost
periodic over short time interval compare to the audio signal without noise, which shows that original
audio signal has very high frequency due to the presence of noise.
While in frequency domain, the noises are clearly can be identified compared to time domain. In
frequency domain, it shows that low frequency signal is being interrupted by high frequencies
approximately at 2240Hz and the signal is symmetric. Therefore, the elimination of noise is implemented
in frequency domain by removing high frequencies (noises that attached to original audio signal) to
retrieve back the original audio signal.
After the IIR filter been applied, again, the same result occur as in FIR, whereby we able to
notice the absence of the highest frequency in the output sample.
iv. FIR vs. IIR
FIR and IIR used to filter audio signal to removed or reduced noise. So, now we can summarize
that which type of digital filter is the best.
Based on our comparison, can obtain that the number of order for FIR is larger than IIR. (In
calculation, Order FIR=342, Order IIR=60). Based on this, we can state that FIR filter need higher order
and trouble-free to apply but FIR does not have feedback, which make FIR stable than IIR. Vice versa,
IIR filter are difficult to implement but they need lower order and not stable. The reason IIR unstable is
because IIR have poles in their transfer function which arise a tendency that the filter can become
unstable. This can be verify in Figure 6, the pole-zero plot graph, we saw the poles may progress to the
outside of the unit circle, and the zeros may spread around z=-1.
The FIR can be designed with exact linear phase, filter structure always stable with quantize
coefficient because FIR have no poles but only have zeros. The purpose of phase linearity is to keep away
from distortions in the output signal, which is in our case, it is an audio signals.
In review, IIR is infinite and used for application where linear characteristic are not of concern.
While FIR are finite which required for linear-phase characteristics. FIR filters are often preferred over
IIR because they are more stable and feedback is not involved.
5.0 CONCLUSION
An audio file explains a format, sometimes referred to as the 'container format', for storing digital
audio data. The audio signals in ".wav" format that was with noise, it was successfully filtered out by
using filtering methods. There are two types of digital filtering techniques we are using, which are Finite
Impulse Response (FIR) and Infinite Impulse Response (IIR). Both filter are suitable to reduce and
removed noise. Firstly have to gather signal characteristics and well understood about the design process
to perform FFT to know the frequency components in signals, design a filter where you can remove
unwanted signals. After that apply filter to our signal to remove unwanted signal or noise. The noises are
successfully removed. It retains the low frequency (bass) and soften the high frequency (treble). We say it
as 'soften' because it is not exactly removed all the noises, it is only reduce the noises. As observation, the
method of filtering noise is depending on the application of the problem.FIR is suitable for linear phase
and stable output, While IIR are suits for always not linear but can be designed to be stable
At the end of these assignments empower us to apply musical capacity of Matlab. We believe
Matlab could be a decent digital music production tool. This is useful for musicians to further discover
their music as well as engineer to better considerate music. To conclude, both FIR and IIR have its
advantages and disadvantages.
6.0 REFERENCES
1. S.K. Mitra, Digital Signal Processing: A Computer-Based Approach, New York, NY: McGraw-Hill, 1998
2. http://en.wikipedia.org/wiki/Filter_(signal_processing)
3. http://dsp.stackexchange.com/questions/9661/estimating-filtered-noise-variance4. Dr. DePiero, Filter Design by Frequency Sampling, CalPoly State University5. http://class.ee.iastate.edu/mmina/ee186/labs/Audio.htm6. W.James MacLean, FIR Filter Design Using Frequency Sampling7. A. V. Oppenheim. (2008). Signals and Systems. Prentice Hall.8. S. D. Stearns and R.A. David. Signal Processing Algorithms in MATLAB. Prentice 9. Hall, 1996. 10. Mathworks Inc., MATLAB Reference Guide, Mathworks, Natick, MA,200011. Lawrence R. Rabiner, Linear Program Design of Finite Impulse Response Digital Filters,
IEEE 197212. Maurice G.Bellanger, Adaptive Digital Filters second edition, Marcel dekker 200113. http://www.cs.tut.fi/sgn/arg/intro/basics.html