Echo generation of FIR sytem

5 views (last 30 days)
Stephen Dokodzo
Stephen Dokodzo on 27 Mar 2021
Commented: Mathieu NOE on 15 Apr 2021
How can I generate some echo to the system above? I was trying this code but no luck.
% Template for the echo cancelling problem
fs = 8000; % sampling frequency (Hz)
f = 400; % sinusoid frequency (Hz)
Tdur = 0.2; % pulse duration (s)
x = pulse(f, Tdur, fs); % Generates a pulse
x = [x zeros(1, 2*length(x))]; % zero pad for processing
n = 0:length(x)-1; % discrete time vector
t = n/fs; % continuous time vector
plot(t, x); % Plot signal
xlabel('Time (s)')
ylabel('Amplitude')
title(sprintf('Original pulse: %.2f Hz', f))
sound(x, fs); % Play the sound
% Zero-pad original pulse
Nzp = floor(fs*Tzp);
nzp = 1:Nzp;
xzp = [x zeros(1,Nzp-length(x))];
figure(2)
plot(nzp/fs,xzp); xlabel('Time (s)'); ylabel('Amplitude');
title(['Original pulse, zero-padded to ' num2str(Tzp,3) ' s'])
pause; sound(xzp);
% Generate echoes
Tdel = 0.1; % echo delay (s)
alpha = 0.5; % echo gain
Ndel = floor(Tdel*fs); % echo delay (samples)
b= [1 zeros(1,Ndel-1) alpha]; a = 1; % filter coefficients
y = filter(b,a,xzp);
figure(3)
plot(nzp/fs,y); xlabel('Time (s)'); ylabel('Amplitude');
title(['Echoed signal, gain = ' num2str(alpha,3)]);
pause; sound(y);
%% Your code goes here
function x = pulse(f, Tdur, fs)
% Sinusoid multiplied by a Hann window function.
% f sinsusoid frequency (Hz)
% Tdur duration (s)
% fs sampling frequency (Hz)
nmax = floor(Tdur*fs); % duration (samples)
n = [1:nmax]; % discrete time index
x = hann(nmax).'.* cos(2*pi*f*n/fs); % waveform samples
end

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Mar 2021
hello
my code as example :
infile='DirectGuitar.wav';
outfile='out_echo.wav';
% read the sample waveform
[x,Fs] = audioread(infile);
% normalize x to +/- 1 amplitude
x = x ./ (max(abs(x)));
% parameters
N_delay=20; % delay in samples
C = 0.7; % amplitude of direct sound
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y = zeros(length(x),1); % create empty out vector
y(1:N_delay)=x(1:N_delay); % to avoid referencing of negative samples
% for each sample > N_delay
for i = (N_delay+1):length(x)
y(i) = C*x(i) + (1-C)*(x(i-N_delay)); % add delayed sample
end
% write output
% normalize y to +/- 1 amplitude
y = y ./ (max(abs(y)));
audiowrite(outfile, y, Fs);
figure(1)
hold on
plot(x,'r');
plot(y,'b');
title('Echoed and original Signal');
sound(y,Fs);
  2 Comments
Thiago de Sousa
Thiago de Sousa on 14 Apr 2021
didn't understand it, can you please fix the code given by the question?
Mathieu NOE
Mathieu NOE on 15 Apr 2021
here you are
% Template for the echo cancelling problem
fs = 8000; % sampling frequency (Hz)
f = 400; % sinusoid frequency (Hz)
Tdur = 0.2; % pulse duration (s)
x = pulse(f, Tdur, fs); % Generates a pulse
x = [x zeros(1, 2*length(x))]; % zero pad for processing
n = 0:length(x)-1; % discrete time vector
t = n/fs; % continuous time vector
plot(t, x); % Plot signal
xlabel('Time (s)')
ylabel('Amplitude')
title(sprintf('Original pulse: %.2f Hz', f))
sound(x, fs); % Play the sound
% Zero-pad original pulse
Tzp = 1; %
Nzp = floor(fs*Tzp);
nzp = 1:Nzp;
xzp = [x zeros(1,Nzp-length(x))];
tzp = (0:length(xzp)-1)/fs;
figure(2)
plot(tzp,xzp); xlabel('Time (s)'); ylabel('Amplitude');
title(['Original pulse, zero-padded to ' num2str(Tzp,3) ' s'])
pause; sound(xzp);
% Generate echoes
Tdel = 0.1; % echo delay (s)
alpha = 0.5; % echo gain
Ndel = floor(Tdel*fs); % echo delay (samples)
b= [1 zeros(1,Ndel-1) alpha]; a = 1; % filter coefficients
y = filter(b,a,xzp);
figure(3)
plot(tzp,y); xlabel('Time (s)'); ylabel('Amplitude');
title(['Echoed signal, gain = ' num2str(alpha,3)]);
pause; sound(y);
%% Your code goes here
function x = pulse(f, Tdur, fs)
% Sinusoid multiplied by a Hann window function.
% f sinsusoid frequency (Hz)
% Tdur duration (s)
% fs sampling frequency (Hz)
nmax = floor(Tdur*fs); % duration (samples)
n = [1:nmax]; % discrete time index
x = hann(nmax).'.* cos(2*pi*f*n/fs); % waveform samples
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!