edit .mat values to match

2 views (last 30 days)
Tatiana
Tatiana on 21 Nov 2023
Answered: Image Analyst on 22 Nov 2023
I currently have this code below:
clc
clear
clear all
[data,fs]=audioread('pracsound.mp3');
save new_file data fs
new_file = new_file';
% Load sample sound
load new_file.mat;
% Play sample sound
sound(data, fs);
% Wait 5 seconds
pause(5)
% Call the function to add echo
y_echo = echo_gen(data, fs, 0.5, 0.75);
% Play echo sound
sound(y_echo, fs);
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
It is supposed to load an .mat audio file play the original, add an echo using a function then play it again.
My problem is the .mat file I want to use appears like this once loaded in:
And I need it to look like this .mat file (A sample that works with my given code found online).
How do I get the data value for new_file to be 246000x1 double like the TestSoundEcho.mat file instead of 246000x2?

Answers (2)

Fangjun Jiang
Fangjun Jiang on 21 Nov 2023
According to "doc audioread",
Audio data in the file, returned as an m-by-n matrix, where m is the number of audio samples read and n is the number of audio channels in the file.
So it was dependent on the contents of the .mp3 file.
You could to this after audioread() to select only one channel
[data,fs]=audioread('pracsound.mp3');
data=data(:,1);
  1 Comment
Walter Roberson
Walter Roberson on 21 Nov 2023
Some people prefer
data = mean(data,2);
to merge all of the channels into a single channel.
For example especially in the 1960's, it was very common in pop and rock music to have one channel (ear) be the vocals, and the other channel (ear) be the music; if you were to just extract one channel from such a recording you would get only the music or only the vocals.

Sign in to comment.


Image Analyst
Image Analyst on 22 Nov 2023
You could try interp1 or imresize to resize the stereo audio signal to be as if it had the number of samples it would have had if it were sampled at 44100 Hz. Attach your audio signal in a .mat file or your mp3 file in a .zip file if you need more help.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!