You are on page 1of 9

Digital Signal Processing

EEE2005 L49+L50
Student Name: Anmol Bansal (17BEE0084)
Faculty Name: Dr. P. Mahalakshmi

Q1) Take two audio signals, mix both of them and design FIR and IIR filters to
remove any one audio signal from the mixed input audio signal, play the single
audio. Compare the performances of the filter design outcomes. (Attach the
audio clips to my email with your Register Number).

Sol.) MATLAB Code:

Fs=44100;
minimumlength=min([length(y1),length(y2)]);
y1=y1(1:minimumlength);
y2=y2(1:minimumlength);
y3=y1+y2;
p=audioplayer(y3,Fs);
play(p);
sound(y3,Fs);
clear sound
h1pf=fdesign.lowpass('Fp,Fst,Ap,Ast',3000,3500,0.5,50,Fs);
D=design(h1pf);
s=filter(D,y3);
sound(s,Fs);
clear sound
subplot(4,1,1);
plot(y1);
title('Music 1')
subplot(4,1,2);
plot(y2);
title('Music 2')
subplot(4,1,3);
plot(y3);
title('Mixed of sound')
subplot(4,1,4);
plot(s);
title('filtered')
stereo_mix=y1;
N=Fs/2;
for n=N+1:length(y1)
stereo_mix(n,1)=y1(n,1)+y1(n-N,1);
stereo_mix(n,2)=y1(n,2)+y1(n-N,2);
end
sound(stereo_mix,Fs);
clear sound;
y1_left=y1(:,1);
y1_right=y1(:,2);
y1_left_minus_y1_right=y1_left-y_right;
sound(y1_left_minus_y1_right,fs);

Screenshots of Output:

First of all we read two audio signals from the directory in which project is
stored. Then we define a threshold length for both sound samples. Then we find
the minimum lengths from both the input samples and trim both of the inputs
to minimum length. Then we merge both of the sounds and play the merged
sound sample. Then we make a low-pass filter using corresponding syntax and
filter the mixed audio sample by this filter. Then we plot all four samples viz
Music 1, Music 2, Mixed Music, Filtered Music. Then we sample both of the
input sound samples and differentiate both of the samples with respect to the
different samples and reconstruct them as two different samples.
Q2) Design a suitable digital filter to remove highest peak from ECG signal (R-
Wave), justify the selection of filter parameters. (Take any ECG signal from net).

Sol.) MATLAB Code:

for demo = 1:2:3
% Clear our variables
clear ecgsamplingratecorrectedfiltered1peaks1filtered2peaks2fresult
switch(demo)
case 1,
plotname = 'Sample 1';
case 3,
plotname = 'Sample 2';
end
% Remove lower frequencies
fresult=fft(ecg);
fresult(1 : round(length(fresult)*5/samplingrate))=0;
fresult(end - round(length(fresult)*5/samplingrate) : end)=0;
corrected=real(ifft(fresult));
% Filter - first pass
WinSize = floor(samplingrate * 571 / 1000);
if rem(WinSize,2)==0
WinSize = WinSize+1;
end
filtered1=ecgdemowinmax(corrected, WinSize);
% Scale ecg
peaks1=filtered1/(max(filtered1)/7);
% Filter by threshold filter
for data = 1:1:length(peaks1)
if peaks1(data) < 4
peaks1(data) = 0;
else
peaks1(data)=1;
end
end
positions=find(peaks1);
distance=positions(2)-positions(1);
for data=1:1:length(positions)-1
if positions(data+1)-positions(data)<distance
distance=positions(data+1)-positions(data);
end
end
% Optimize filter window size
QRdistance=floor(0.04*samplingrate);
if rem(QRdistance,2)==0
QRdistance=QRdistance+1;
end
WinSize=2*distance-QRdistance;
% Filter - second pass
filtered2=ecgdemowinmax(corrected, WinSize);
peaks2=filtered2;
for data=1:1:length(peaks2)
if peaks2(data)<4
peaks2(data)=0;
else
peaks2(data)=1;
end
end
% Create figure - stages of processing
figure(demo); set(demo, 'Name', strcat(plotname, ' - Processing Stages'));
% Original input ECG data
subplot(3, 2, 1); plot((ecg-min(ecg))/(max(ecg)-min(ecg)));
title('\bf1. Original ECG'); ylim([-0.2 1.2]);
% ECG with removed low-frequency component
subplot(3, 2, 2); plot((corrected-min(corrected))/(max(corrected)-min(corrected)));
title('\bf2. FFT Filtered ECG'); ylim([-0.2 1.2]);
% Filtered ECG (1-st pass) - filter has default window size
subplot(3, 2, 3); stem((filtered1-min(filtered1))/(max(filtered1)-min(filtered1)));
title('\bf3. Filtered ECG - 1^{st} Pass'); ylim([0 1.4]);
% Detected peaks in filtered ECG
subplot(3, 2, 4); stem(peaks1);
title('\bf4. Detected Peaks'); ylim([0 1.4]);
% Filtered ECG (2-d pass) - now filter has optimized window size
subplot(3, 2, 5); stem((filtered2-min(filtered2))/(max(filtered2)-min(filtered2)));
title('\bf5. Filtered ECG - 2^d Pass'); ylim([0 1.4]);
% Detected peaks - final result
subplot(3, 2, 6); stem(peaks2);
title('\bf6. Detected Peaks - Finally'); ylim([0 1.4]);
% Create figure - result
figure(demo+1); set(demo+1, 'Name', strcat(plotname, ' - Result'));
% Plotting ECG in green
plot((ecg-min(ecg))/(max(ecg)-min(ecg)), '-g'); title('\bf Comparative ECG R-Peak
Detection Plot');
% Show peaks in the same picture
hold on
% Stemming peaks in dashed black
stem(peaks2'.*((ecg-min(ecg))/(max(ecg)-min(ecg)))', ':k');
% Hold off the figure
hold off
end

MATLAB Code for Windowed Filter:

% ECGDEMO ECG PROCESSING DEMONSTRATION - R-PEAKS DETECTION
%
% This file is a part of a package that contains 5 files:
%
% 1. ecgdemo.m - main script file;
% 2. ecgdemowinmax.m - (this file) window filter script file;
% 3. ecgdemodata1.mat - first ecg data sample;
% 4. ecgdemodata2.mat - second ecg data sample;
%
% To run the demo put
%
% ecgdemo.m;
% ecgdemowinmax.m;
% ecgdemodata1.mat;
% ecgdemodata2.mat;
%
% in MatLab's "work" directory, run MatLab and type in
%
% >> ecgdemo
%
% The code is property of LIBROW
% You can use it on your own
% When utilizing credit LIBROW site
function Filtered=ecgdemowinmax(Original, WinSize)
WinHalfSize = floor(WinSize/2);
WinHalfSizePlus = WinHalfSize+1;
WinSizeSpec = WinSize-1;
FrontIterator = 1;
WinPos = WinHalfSize;
WinMaxPos = WinHalfSize;
WinMax = Original(1);
OutputIterator = 0;
for LengthCounter = 0:1:WinHalfSize-1
if Original(FrontIterator+1) > WinMax
WinMax = Original(FrontIterator+1);
WinMaxPos = WinHalfSizePlus + LengthCounter;
end
FrontIterator=FrontIterator+1;
end
if WinMaxPos == WinHalfSize
Filtered(OutputIterator+1)=WinMax;
else
Filtered(OutputIterator+1)=0;
end
OutputIterator = OutputIterator+1;
for LengthCounter = 0:1:WinHalfSize-1
if Original(FrontIterator+1)>WinMax
WinMax=Original(FrontIterator+1);
WinMaxPos=WinSizeSpec;
else
WinMaxPos=WinMaxPos-1;
end
if WinMaxPos == WinHalfSize
Filtered(OutputIterator+1)=WinMax;
else
Filtered(OutputIterator+1)=0;
end
FrontIterator = FrontIterator+1;
OutputIterator = OutputIterator+1;
end
for FrontIterator=FrontIterator:1:length(Original)-1
if Original(FrontIterator+1)>WinMax
WinMax=Original(FrontIterator+1);
WinMaxPos=WinSizeSpec;
else
WinMaxPos=WinMaxPos-1;
if WinMaxPos < 0
WinIterator = FrontIterator-WinSizeSpec;
WinMax = Original(WinIterator+1);
WinMaxPos = 0;
WinPos=0;
for WinIterator = WinIterator:1:FrontIterator
if Original(WinIterator+1)>WinMax
WinMax = Original(WinIterator+1);
WinMaxPos = WinPos;
end
WinPos=WinPos+1;
end
end
end
if WinMaxPos==WinHalfSize
Filtered(OutputIterator+1)=WinMax;
else
Filtered(OutputIterator+1)=0;
end
OutputIterator=OutputIterator+1;
end
WinIterator = WinIterator-1;
WinMaxPos = WinMaxPos-1;
for LengthCounter=1:1:WinHalfSizePlus-1
if WinMaxPos<0
WinIterator=length(Original)-WinSize+LengthCounter;
WinMax=Original(WinIterator+1);
WinMaxPos=0;
WinPos=1;
for WinIterator=WinIterator+1:1:length(Original)-1
if Original(WinIterator+1)>WinMax
WinMax=Original(WinIterator+1);
WinMaxPos=WinPos;
end
WinPos=WinPos+1;
end
end
if WinMaxPos==WinHalfSize
Filtered(OutputIterator+1)=WinMax;
else
Filtered(OutputIterator+1)=0;
end
FrontIterator=FrontIterator-1;
WinMaxPos=WinMaxPos-1;
OutputIterator=OutputIterator+1;
End
Output Snapshots:
Filtering method depends on the type of noises in ECG signal. In some signals
the noise level is very high and it is not possible to recognize it by single
recording, it is important to gain a good understanding of the noise processes
involved before one attempt to filter or pre-process a signal. The ECG signal is
very sensitive in nature, and even if small noise mixed with original signal the
characteristics of the signal changes. Data corrupted with noise must either
filtered or discarded, filtering is important issue for design consideration of real
time heart monitoring systems.
Here we first convert the signals to frequency domain using FFT, then we
remove the low frequency components and convert it into time domain again
using IFFT.
Then we find the local maxima using a Windowed filter.
Then we remove the small components and keep the significant ones. Then we
adjust the filter size and repeat above steps to achieve the answer.

BIBLIOGRAPHY:
1. http://www.librow.com/articles/article-13
2. https://matlab.mathworks.com/
3. http://physionet.org/physiobank/database/#ecg