How can I get these to match
2 views (last 30 days)
Show older comments
My code is below:%Create a echo version
%Clear all load ins
clc
clear
clear all
%Load in practice audio
[data,fs]=audioread('pracsound.mp3');
data=data(:,1);
samples = [1,5*fs];
[data,fs]=audioread('pracsound.mp3',samples);
data=data(:,1);
save new_file data fs
% Load Practice sound
load new_file.mat;
% Play Practice sound
sound(data, fs);
% Wait 6 seconds, allows original to finish playing
pause(6)
% Call the function to add echo to practice sound
y_echo = echo_gen(data, fs, 0.2, .75);
% Play new echo sound
sound(y_echo, fs);
% Plot original vs Echo sound
% t=0:seconds(1/fs):seconds(5);
% t = t(1:end-1);
% plot(t,data)
%Function to add echo to sound
function output=echo_gen(input, fs, delay, amp)
% Preallocate new vector
output=zeros((numel(input)+delay*fs),1);
% Add zeros to input to get both vectors same length
input = [input; zeros(delay*fs,1)];
% No change during the delay time
output(1:delay*fs) = input(1:delay*fs);
% Add Echo to the remaining part
output(delay*fs+1:end)= input(1:end-delay*fs)*amp + input(delay*fs+1:end);
% Normalize between -1 and 1
output = rescale(output,-1,1);
end
I am trying to get my data and y_echo values to match exactly so I am able to plot them against one another.
Can someone please tell me how to either force them to be the same size and/ or provide code that will graph them.
2 Comments
Dyuman Joshi
on 21 Nov 2023
Edited: Dyuman Joshi
on 21 Nov 2023
This question seems like a duplicate of https://in.mathworks.com/matlabcentral/answers/2050382-edit-mat-values-to-match?s_tid=prof_contriblnk
Answers (1)
Voss
on 21 Nov 2023
Edited: Voss
on 21 Nov 2023
y_echo = echo_gen(data, fs, 0.2, .75);
After that line, y_echo is longer than data, as you observe.
One way to make them the same size is to append zeros to data so that it is extended to the size of y_echo. One way to do that is:
data(end+1:size(y_echo,1),:) = 0;
Of course, they don't have to be the same size in order to plot them with each other; they just have to each be plotted against an appropriately-sized time vector. That is, you can use one time vector for plotting y_echo and a different time vector for plotting data.
0 Comments
See Also
Categories
Find more on Audio I/O and Waveform Generation 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!