How to adds an echo effect to an audio recording

6 views (last 30 days)
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.)

Answers (1)

Tridib
Tridib on 26 Mar 2025
Here is a code example to get you started on how to add echo effect to an audio recording:
function output = echo_gen(input, fs, delay, amp)
% Number of samples corresponding to the specified
% delay in seconds is calculated using the following formula
delay_samples = round(delay * fs);
% Output vector's length will contain the original signal plus the delayed echo
output_length = length(input) + delay_samples;
output = zeros(output_length, 1);
% Original input signal is copied to the output vector
output(1:length(input)) = input;
% This loop iterates over the input signal, adding the echo to the output
% at appropriate delayed position (offset by delay_samples)
% Echo is scaled by the amp factor
for i = 1:length(input)
echo_index = i + delay_samples;
if echo_index <= output_length
output(echo_index) = output(echo_index) + amp * input(i);
end
end
% If any values in the output exceed the range [-1, 1],
% the entire output is normalized to ensure all values are within this range
max_val = max(abs(output));
if max_val > 1
output = output / max_val;
end
end
To test the above function, use the following commands in the MATLAB command window:
load gong
sound(y, Fs) % plays original sound
delay = 0.5;
amp = 0.6;
output = echo_gen(y, Fs, delay, amp);
% Plays the sound with echo
sound(output, Fs)
For more information on the ‘sound’ function, refer to the following documentation:
Hope this helps!

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!