# Digital Signal Processing Filters

5 views (last 30 days)
Mustafa Kemal Tasci on 25 Dec 2022
Hello, I am new to Matlab. I have problems could you help me with these ? I recorded my speech that said "Hello Friend" in a noisy environment. I want to design a filter that reduces noise and uses group delay to observe "Friend Hello" at the output. I designed an arbitrary filter to observe what is happening I have an issue also in the graph of frequency axises are wrong filter parameters and they are not match
Here are my codes:
clc
close all
clear
[N,P] = size(y);
ts = 1/Fs; % ts is the sampling period
tmax= (N-1)*ts; % tmax is the maxmimum period
t = 0:ts:tmax; % Defines a vector t that starts from 0 upto tmax increaments by the amount of sampling period
f = 0:pi/N:pi-pi/N; % For plotting the frequency spectrum
figure("Name","Time Domain Analysis")
subplot(2,1,1)
plot(t,y)
title("Unfiltered Signal in Time"); xlim([0, tmax]); xlabel("Time (sec)"); ylabel("Magnitude")
%%sound(y, Fs)
z = filter(Filter,y);
subplot(2,1,2)
plot(t,z)
title("Filtered Signal in Time"); xlim([0, tmax]); xlabel("Time (sec)"); ylabel("Magnitude")
sound(z, Fs)
Y = fftshift(fft(y));
figure("Name", "Frequency Domain Analysis")
subplot(2,1,1);
plot(f/pi,20*log10(abs(Y)))
title("Unfiltered Signal in Frequency"); xlabel("Frequency (Hz)"); ylabel("Magnitude (dB)")
Z = fftshift(fft(z))
subplot(2,1,2);
plot(f/pi,20*log10(abs(Z)))
title("Filtered Signal in Frequency"); xlabel("Frequency (Hz)"); ylabel("Magnitude (dB)")
fvtool(Filter)
freqz(Filter)
title("Magnitude Response")
subplot(2,1,2)
plot("""");
title("Phase Response")
function Hd = Filter
%FILTER Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.11 and Signal Processing Toolbox 8.7.
% Generated on: 25-Dec-2022 22:16:37
% Equiripple Bandpass filter designed using the FIRPM function.
% All frequency values are normalized to 1.
Fstop1 = 0.09; % First Stopband Frequency
Fpass1 = 0.12; % First Passband Frequency
Fpass2 = 0.25; % Second Passband Frequency
Fstop2 = 0.3; % Second Stopband Frequency
Dstop1 = 0.001; % First Stopband Attenuation
Dpass = 0.057501127785; % Passband Ripple
Dstop2 = 0.0001; % Second Stopband Attenuation
dens = 20; % Density Factor
% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fstop1 Fpass1 Fpass2 Fstop2], [0 1 0], ...
[Dstop1 Dpass Dstop2]);
% Calculate the coefficients using the FIRPM function.
b = firpm(N, Fo, Ao, W, {dens});
Hd = dfilt.dffir(b);
% [EOF]
end

Paul on 25 Dec 2022
Edited: Paul on 25 Dec 2022
Hi Mustafa,
Not checking all of the code in detail, but I did notice that the fft plots don't look correct. They should be:
f = ceil(linspace(-N/2,N/2-1,N))/N*Fs; % Hz
Y = fftshift(fft(y));
figure("Name", "Frequency Domain Analysis")
subplot(2,1,1);
%plot(f/pi,20*log10(abs(Y)))
plot(f,20*log10(abs(Y)))
title("Unfiltered Signal in Frequency"); xlabel("Frequency (Hz)"); ylabel("Magnitude (dB)")
Z = fftshift(fft(z))
subplot(2,1,2);
%plot(f/pi,20*log10(abs(Z)))
plot(f,20*log10(abs(Z)))
title("Filtered Signal in Frequency"); xlabel("Frequency (Hz)"); ylabel("Magnitude (dB)")
Also, can this statement be clarified: "uses group delay to observe "Friend Hello" at the output." I'm not sure how group delay relates to swapping two portions of the signal.
##### 1 CommentShowHide None
Mustafa Kemal Tasci on 26 Dec 2022
Thank you Paul. I want to design a filter that removes the noise and delays the "Hello" part but I don't know. In theory, if I add a group delay to the frequency range where "Hello" is located, "Hello" will be delayed in time.
This is the my professor's example code :
clear all
close all
clc
%%% signal generation %%%
f1=5; %frequency1
f2=12; %frequency2
fs=100; %sampling frequency
ts=1/fs; %sampling interval
t=1:ts:5; %speech duration in s
x=[zeros(400,1); sin(t*2*pi*f1)'; zeros(200,1); sin(t*2*pi*f2)'; zeros(3000,1)];
lx=length(x);
y=x+0.1*randn(lx,1); y=y/max(y);
figure(1), plot(y)
%%% signal analysis %%%
% N=2^15;
N=lx;
y_f=fft(y,N);
y_f_a=abs(y_f);
eks=fs*(0:N/2)/N;
figure(2), plot(eks,20*log10(y_f_a(1:N/2+1).^2))
%%% filter design %%%
b1=fir1(50,[10 15]/(fs/2),'bandpass');
[H1,w1] = freqz(b1,1,N,'whole');
H = (H1(1:N/2)); w = (w1(1:N/2));
mag = abs(H);
db = 20*log10((mag)/max(mag));
pha = angle(H);
grd = grpdelay(b1,1,w);
figure(3), plot(w/pi,db);
figure(4), plot(w/pi,grd);
yfil1=filter(b1,1,y);
b2=fir1(3000,[3 8]/(fs/2),'bandpass');
yfil2=filter(b2,1,y);
yfil=yfil1+yfil2;
figure(5), plot(yfil)
yfil_f=fft(yfil,N);
yfil_f_a=abs(yfil_f);
figure(6),
plot(eks,20*log10(yfil_f_a(1:N/2+1).^2))

### Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by