Echo generation of FIR sytem

조회 수: 6 (최근 30일)
Stephen Dokodzo
Stephen Dokodzo 2021년 3월 27일
댓글: Mathieu NOE 2021년 4월 15일
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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 3월 29일
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
Thiago de Sousa
Thiago de Sousa 2021년 4월 14일
didn't understand it, can you please fix the code given by the question?
Mathieu NOE
Mathieu NOE 2021년 4월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Switches and Breakers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by