I meet some problem in my coursera homework Echo Generator

조회 수: 9 (최근 30일)
bingnan lee
bingnan lee 2019년 9월 5일
답변: zaber mhamud 2022년 6월 22일
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 .

채택된 답변

Steven Lord
Steven Lord 2019년 9월 5일
I'm going to comment on individual pieces of your code to offer suggestions and avenues for investigation.
function [output]= echo_gen(input,fs,delay,amp)
[r,c] = size(input);
add = round(delay*fs);
vdm = zeros(r+add,1);
otp = vdm;
I'm not that familiar with audio processing, so I don't know if "vdm" and "otp" are technical terms of art in that area, but I'd encourage you to use longer, more descriptive names. This may help you and people reading your code in the future understand what your code is doing. If I understand your code correctly, I'd call vdm something like echoSignal and otp something like originalSignal. [If you're worried about typing long names over and over, tab completion can help with that.]
Adding comments describing the purpose of each step (not so much what the code is doing, but why it's doing what it's doing) may also help you keep track of your algorithm. So the comment for this line:
vdm = zeros(r+add,1);
wouldn't be just "Creates a zeros vector" but "Create the vector to store the echo". This may feel redundant if you renamed variables to something like:
echoSignal = zeros(r + extraEchoTime, 1);
but you could write the comments describing the steps in the algorithm before you write your code which could help you break your problem into manageable pieces.
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);
Since jugg is a vector, you don't really need the dimension argument in your call to max.
if t>1
otp = otp/t;
If t is greater than 1, you never assign a value to the variable output and so MATLAB should throw an error.
Also how do you define "normalization"? Is it division by the maximum absolute value of elements in your array, or is it more along the lines of the 'range' option for the normalize function? Using the "Scale Data" example on that page, which of the following should the "normalization" of A be?
A = 1:5;
bingnanNormalization = A./max(abs(A))
rangeNormalization = normalize(A, 'range', [-1 1])
One final suggestion: as the assignment suggests, "% sound (newY, Fs) % Uncomment in MATLAB to listen to the new sound data". Play the unaltered sound and the sound with the echo and see if it sounds to your ear like there's an echo. If it doesn't, perhaps the way the altered sound sounds will give you a clue as to what's wrong.
  댓글 수: 6
Sana Hafeez
Sana Hafeez 2021년 6월 3일
@bingnan lee Please help me to find out the correct code of image blur
I am stuck now!
Steven Lord
Steven Lord 2021년 6월 3일
This doesn't look like it's related to the original question about creating a function to add an echo to an audio recording so you should ask it as a new question with a descriptive subject, the code pasted into the question rather than posted as a picture (since it's hard to run a picture), a clear description of what the code is intended to do, and a clear statement of the specific problem you're experiencing when trying to run this code.

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

추가 답변 (3개)

Prashant Dubey
Prashant Dubey 2020년 5월 10일
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

Abdul Quadir Khan
Abdul Quadir Khan 2020년 11월 6일
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
zaber mhamud 2022년 6월 22일
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

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by