Convert .csv to .wav audio

I am fairly new to matlab
I wanted to convert a comma separated value (.csv) file to a Wave (.wav) audio file while also plotting it.
I saw this previously answered Here on matlab answers but I had a few questions and some errors, for the sake of simplicity I have attached the code and the csv file here
error shown is
Undefined function or variable 'y'.
Error in Untitled (line 17)
audiowrite('audio_output.wav',y,Fs)
and I did not understand why in the code the variable 'y' and 'Fs' are there in code
clc;
clear all;
close all;
data=csvread("output.csv");
time=data(: ,1);
output=data(: ,2);
plot(time,output);
save('aec.mat','time','output');
load aec.mat
filename='audio_output.wav';
Fs=6000;
audiowrite('audio_output.wav',y,Fs)
clear y Fs
[y,Fs]=audioread(filename);
sound(y,Fs);

 Accepted Answer

Geoff Hayes
Geoff Hayes on 16 Apr 2020
Edited: Geoff Hayes on 16 Apr 2020
Debagnik - the y variable represents the audio samples. In your case, that would be output. Just change your code to the following
time = data(: ,1);
y = data(: ,2);
plot(time, y);
I see that you have set the sampling rate, Fs, to be 6000...just like in the link that you have provided. If your time data is in seconds, then it looks like you have 2.5 seconds (time(end) - time(1)) worth of data. Since there are 108569 (length(output)) samples, then this would mean your Fs could be
Fs = length(output) / (time(end) - time(1)); % 43427.6
Is this correct? Or is your sampling freqency really 6000?

4 Comments

I didn't knew what was Fs variable so I used the same data. And you have guessed correctly that my time data is of 2.5 seconds, so I implemented all the chnges you suggested and the script run well but there were warnings
Warning: Data clipped when writing file.
> In audiowrite>clipInputData (line 396)
In audiowrite (line 176)
In Untitled (line 18)
And I don't know about the function of matlab but I guess. If I have provided a data worth of 2.5s then the audio file should also be of 2.5s but in the exported file it is a mere in milliseconds
here is the updated code
clc;
clear all;
close all;
data=csvread("output.csv");
time=data(: ,1);
output=data(: ,2);
plot(time,output);
Fs=length(output)/(time(end)-time(1));
save('aec.mat','Fs','data');
load aec.mat
filename='audio_output.wav';
audiowrite('audio_output.wav',output,Fs)
clear output Fs
[output,Fs]=audioread(filename);
sound(output,Fs);
followup new errors
Error using audiowrite
The value of 'Fs' is invalid. Expected input to be integer-valued.
Error in audiowrite>parseInputs (line 226)
parse(p,filename, y, Fs, pvpairs{:});
Error in audiowrite (line 103)
props = parseInputs(filename, y, Fs, varargin);
Error in Untitled (line 18)
audiowrite('audio_output.wav',output,Fs)
another followup
as per the first error in the previous error that they expected the sample rate to be integer so I used round() function in that line
Fs=round(length(data)/(time(end)-time(1)); %43428
hence mostly all my probles are solved
except this warning.
Warning: Data clipped when writing
file.
> In audiowrite>clipInputData (line 396)
In audiowrite (line 176)
In Untitled (line 18)
From audiowrite y parameter, The valid range for the data in y depends on the data type of y. For data types double (your input) the range is -1.0 to +1.0. Your input is between -6.5 and 6.0. I suppose that you could divide your data y by the maximum absolute value:
time = data(: ,1);
y = data(: ,2);
y = y / max(abs(min(y)),max(y));

Sign in to comment.

More Answers (2)

clc;
clear all;
close all;
y=csvread("original.csv");
display(y)
filename='yes.wav';
Fs=16000;
audiowrite(filename,y,Fs)
clear y Fs
[y,Fs]=audioread(filename);
display(y)
display(Fs)
sound(y,Fs);
The time in the file is not uniformly sampled. The data needs to be resampled before being written to a file. Also, the file contains duplicate times with different data.
From previous discussion and the evidence of the file, we know that the file is intended to represent 2.5 seconds. The number of entries in the file is about 7 1/2 times greater than 2.5 seconds * 6000 samples/second .
We cannot just resample because the input is not regular timesteps. We cannot just use fft methods either for the same reason. We cannot use interp1 because of the duplicate times.
I experimented with nufft but the results I got back were suspicious, and I do not trust them yet.
data = csvread("output.csv");
time = data(: ,1);
output = data(: ,2);
filename='audio_output.wav';
Fs=6000;
TT = timetable(output, 'RowTimes', seconds(time));
TT = retime(TT, 'regular', 'mean', 'TimeStep', seconds(1/Fs));
t = TT.Properties.RowTimes;
y = rescale(TT.output, -1, 1);
audiowrite('audio_output.wav', y, Fs)
plot(t, y)
sound(y, Fs);

Products

Release

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!