Echo generator problem - Coursera Introduction to Matlab Programming

Hey all,
I am trying to program the solution to the Echo Blur problem (I have attached a screenshot below describing the task):
My code is written below, could anyone give me any pointers as to where I am going wrong?
function[output] = echo_gen(input, fs, delay, amp)
input = input'; %storing the column vector input as a row vector
samplesinthesrecording = length(input); %finding the number of samples in the input vector
numberofdelaysamples = round(delay)*fs; %finding the number of samples in the delay
sampleatwhichechostarts = numberofdelaysamples + 1; %finding the sample number at which the echo begins
soundbeforecho = input(input < input(sampleatwhichechostarts)); % the sound before the echo is playing during the samples which appear before the echo
amplifiedecho = input*amp; %the echo is amplified by an amount specified by amp
originalsoundplusecho = [soundbeforecho, amplifiedecho]; %I am binding the vector of samples for both the original sound and the echo
if min(originalsoundplusecho) < -1 || max(originalsoundplusecho) > 1 %I am making sure the values within the vector 'originalsoundplusecho' are less than 1 and greater than -1
normalisedvector = rescale(originalsoundplusecho,-1,1); %I am normalising the vector if the condition above is not met, making sure it is in the boundaries -1 to 1
output = normalisedvector'; %the output is the normalised vector
else
output = originalsoundplusecho'; %if the values of the vector 'originalsoundplusecho' are within -1 to 1, then the output is the vector 'originalsoundplusecho'
end
end
Here is the code to call the function:
load splat
output = echo_gen(y, Fs, 0.25, 0.6); %calling the function
dt = 1/Fs; %the interval between values on the x-axis
t = 0:dt:dt*(length(output)-1);%the x-axis scale
plot(t, output) %plotting the graph
Thank you very much!

8 Comments

What leads you to think that you have errors in the code?
@Walter Roberson, there are no such errors in the code itself, but the program is telling me that my code is not accepted for the following reasons:
Tested with the vector [-0.5; 0; 0.5; 0] and the following parameters: fs = 1, delay: 0.0 seconds, amp = 0.5 - this gave the incorrect output
Variable output must be of size [10820 1]. It is currently of size [16055 1]. Check where the variable is assigned a value. Tested with the splat file and the following parameters: fs = 8192, delay: 0.1 seconds, amp = 0.0 - it also gave me this message.
This is the output the code is giving me: (Screenshot)
@Walter Roberson could you give me any advice on what might be going wrong?
soundbeforecho = input(input < input(sampleatwhichechostarts)); % the sound before the echo is playing during the samples which appear before the echo
input(sampleetc) is the value at that location. input < that is a logical vector with true for all locations whose value is less than that particular value, including samples before and after the start of the echo. input() indexed at that selects the content of all samples whose value happens to be less than the value at the start of the echo.
I do not understand what possible use such a vector would be for this project.
if you want to select the first 10 samples then input(1:10)
please do not use input as the name of a variable, as it confuses readers who expect input to refer to the matlab function to ask the user to enter data.
@Walter Roberson, in the function:
soundbeforecho = input(input < input(sampleatwhichechostarts));
In am selecting the samples which come before the echo from the 'input' column vector.
I am then binding this sample to a vector with the samples within the echo:
originalsoundplusecho = [soundbeforecho, amplifiedecho];
So effectively, the output of the function should be the values of the y-axis which correspond to the sound being replayed and then the echo of the sound (with an amplification).
In am selecting the samples which come before the echo from the 'input' column vector.
No, you are not. Suppose for example
input = [3 1 4 1 5 9 2 6 5 3 5];
sampleatwhichechostarts = 9;
input(sampleatwhichechostarts)
input < input(sampleatwhichechostarts)
input(input < input(sampleatwhichechostarts))
ans =
5
ans =
1×11 logical array
1 1 1 1 0 0 1 0 0 1 0
ans =
3 1 4 1 2 3
When what you want is [3 1 4 1 5 9 2 6] which is input(1:8)
@Walter Roberson, thank you very much!
The code below works!
function[output] = echo_gen(input, fs, delay, amp)
numberofdelaysamples = round(delay*fs);
soundvectorplusecho = zeros(numberofdelaysamples + length(input),1);
soundvectorbeforecho = soundvectorplusecho;
for i = 1:length(input)
soundvectorplusecho(i + numberofdelaysamples) = input(i)*amp;
soundvectorbeforecho(i) = input(i);
end
fullsoundvector = soundvectorbeforecho + soundvectorplusecho;
range = abs(fullsoundvector);
maxrange = max(range);
if maxrange>1
fullsoundvector = fullsoundvector/maxrange;
end
output = fullsoundvector;
end

Sign in to comment.

Answers (1)

function output = echo_gen(input, fs, delay, amp)
delay_mat=zeros(round(delay*fs),1); % a col vector.
echo_mat=([delay_mat;input])*amp; % make echo col vector,input is a column vector.
output=input+echo_mat(1:length(input),1); % super imposing echo with origional input sound track.
output=[output;echo_mat(length(input)+1:end,1)];
if max(abs(output))>1 % scaling b/w -1,+1 throught relative scaling.
output=output/max(abs(output));
end
end

Tags

Asked:

on 18 Apr 2020

Commented:

on 1 Aug 2023

Community Treasure Hunt

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

Start Hunting!