Clear Filters
Clear Filters

How can I get these to match

2 views (last 30 days)
Tatiana
Tatiana on 21 Nov 2023
Edited: Tatiana on 21 Nov 2023
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
Tatiana
Tatiana on 21 Nov 2023
Edited: Tatiana on 21 Nov 2023
I need to now get the two values to match, The previous question was how to get the data value to be and x1 array. since y_echo is not a file its just a manipulated audio I don;t know how to manipulate the same as a actual file.

Sign in to comment.

Answers (1)

Voss
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.

Categories

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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!