I meet some problem in my coursera homework Echo Generator
Show older comments
Write a function called echo_gen that adds an echo effect to an audio recording. The function is to be called like this:
output = echo_gen(input, fs, delay, amp);
where input is a column vector with values between -1 and 1 representing a time series of digitized sound data. The input argument fs is the sampling rate. The sampling rate specifies how many samples we have in the data each second. For example, an audio CD uses 44,100 samples per second. The input argument delay represent the delay of the echo in seconds. That is, the echo should start after delay seconds have passed from the start of the audio signal. Finally, amp specifies the amplification of the echo which normally should be a value less than 1, since the echo is typically not as loud as the original signal.
The output of the function is a column vector containing the original sound with the echo superimposed. The output vector will be longer than the input vector if the delay is not zero (round to the nearest number of points needed to get the delay, as opposed to floor or ceil). A sound recording has values between -1 and 1, so if the echo causes some values to be outside of this range, you will need to normalize the entire vector, so that all values adhere to this requirement.
MATLAB has several sample audio files included that you can try: splat, gong, and handel are a few examples. Try the following:
load gong % loads two variables, y and Fs
sound(y, Fs) % Outputs sound
To hear the sound you will need to use desktop MATLAB or MATLAB Online.
(Note that we are assuming mono audiofiles. You can load your own audio files using the audioread function
Opens in new tab
in MATLAB. If the audio data has two columns, it is a stereo file, so use only one column of the data when testing your file.)
Code to call your function
% Load splat which adds y and Fs to the workspace
load splat
% Call echo_gen to create the new audio data
output = echo_gen(y, Fs, 0.25, 0.6);
% Create a time axis. The time between points is 1/Fs;
dt = 1/Fs;
t = 0:dt:dt*(length(output)-1);
% Plot the new data to see visualize the echo
plot(t, output)
% sound (newY, Fs) % Uncomment in MATLAB to listen to the new sound data
here is my code
function [output]= echo_gen(input,fs,delay,amp)
[r,c] = size(input);
add = round(delay*fs);
vdm = zeros(r+add,1);
otp = vdm;
for i=1:r
vdm(i+add,1) =input(i,1)*amp ;
otp(i) = input(i);
end
otp = otp+vdm;
jugg = abs(otp) ;
t = max(jugg,[],2);
if t>1
otp = otp/t;
end
output = otp ;
end

Can anyone please tell me where I am going wrong with the code for this question? I have no idea how to solve the problem .
Accepted Answer
More Answers (3)
Prashant Dubey
on 10 May 2020
function output = echo_gen(s, Fs, delay, amp)
% Find the time between points using Fs
dt = 1/Fs;
% Calculate the number of points for the given delay
N = round(delay/dt);
% Pad the original signal with zeros to make room for the echo
s1 = [s; zeros(N, 1)];
% Create an echo signal that starts with 0's
s2 = [zeros(N, 1); s.*amp];
% Add the echo to the original signal
output = s1 + s2;
% the abs of all values must be < 1. Rescale if necessary
if max(abs(output)) > 1
output = output./max(abs(output));
end
% Note: This only works with column vectors - can you make the
% function more robust so that it works with column or row vectors?
end
2 Comments
Prashant Dubey
on 10 May 2020
This is the solution of this problem
Mark Honey Ancheta
on 7 Jul 2021
this!!
Abdul Quadir Khan
on 6 Nov 2020
function [output]= echo_gen(input,fs,delay,amp)
[r,c] = size(input);
extraEchoTime = round(delay*fs);
echoSignal = zeros(r+extraEchoTime,1);
addEchoSignal = echoSignal ;
for i=1:r
echoSignal(extraEchoTime+i,1) =input(i,1)*amp ;
addEchoSignal(i) = input(i);
end
addEchoSignal = addEchoSignal + echoSignal ;
range = abs(addEchoSignal) ;
maxrange = max(range);
if maxrange>1
addEchoSignal = addEchoSignal/maxrange;
end
output = addEchoSignal ;
end
zaber mhamud
on 22 Jun 2022
function output = echo_gen(input,fs,delay,amp)
sounds = round(fs * delay) ;
ds = floor(sounds);
sig = zeros(length(input) + ds, 1);
sig(1:length(input)) = input;
e_sig = zeros(length(input) + ds, 1);
e_sig(ds + (1:length(input*amp))) = input * amp;
output = sig + e_sig;
maxx = max(abs(output));
if maxx > 1
output = output ./ maxx;
end
end
Categories
Find more on Windows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!