How to convert an excel file (.csv) to a .wav format/any other audio format file?

Hi,
I have an excel file which has ~200k data samples stored in one column and I want to convert the data from that file into an audio file. But whenever I use the excel file, there is almost no sound. I want to play it at 40khz frequency.
I am just using this code:
clearvars
a=xlsread('w3.csv'); %read the xls table
figure,plot(a) %this is your signal
xlim([0 200704])
sound(a,40000) %play the signal
audiowrite('w3.wav',a,40000,'BitsPerSample',16);
Can someone help? Thanks.

 Accepted Answer

I was able to play your audio data and save it as a .wav file. It's some sort of noise along with a dripping sound. But, I had to first rescale the data to fall between -1 and 1:
clearvars
a = xlsread('temp.csv'); % this is your w3.csv file
a = rescale(a, -1, 1);
figure,plot(a) %this is your signal
xlim([0 200704])
sound(a,40000) %play the signal
audiowrite('w3.wav',a,40000,'BitsPerSample',16);

1 Comment

Hi,
Yes! Thanks, it the sound of waterflow from a pipe. However, I wanted to know the logic behind that why would I need to convert it to -1 to +1. I also checked other ranges such as 0 to 1 or -5 to +5 or -10 to 10 and they all gave expected results.

Sign in to comment.

More Answers (1)

This
should give a hint that xlsread is not the right way to go. In this case, you want to use readmatrix:
a = readmatrix("w3.csv")

5 Comments

Hi Sir,
Thanks for the suggestion. However, when I use the same code and only change xlsread to readmatrix, I get the following error which does not appear when I am using xlsread. How do I fix this issue? Thanks.
Error in filtfilt>efiltfilt (line 114)
validateattributes(x,{'double'},{'finite','nonempty'},'filtfilt');
Error in filtfilt (line 89)
y=efiltfilt(b,a,x);
Error in binarytowav (line 9)
y_h2 = filtfilt(sos, g, m);
Below is my code:
clearvars
n= readmatrix("3.csv"); %read the xls table
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.47; %same way but in same line
fs=40000;
[z,p,k] = ellip(4,1,40,2400/(fs/2),'high'); %z,p,k method
[sos,g] = zp2sos(z,p,k);
y_h2 = filtfilt(sos, g, m);
figure,plot(m) %this is your signal
ylim([-0.5 0.5])
xlim([0 200704])
xlabel('Time (sec)');
ylabel('Amplitude');
title('Stage1 PCB, waterflow: >3gpm');
AmplitudeWaterflow=sum(abs(y_h2))
x = gca;
tick_scale_factor = 1/40000;
ax.XTickLabel = ax.XTick * tick_scale_factor;
sound(m,40000)
@Giggs B. You've got the wrong filename in your script. The file is "w3.csv", not "3.csv". Fix that typo and it should work fine.
@Scott MacKenzie, Sorry for the confusion. Actually i have lots of files. I used the correct file for which I was checking, but that error is persistant. For ex. please check my image, I am checking file "5.csv" but same error. And when I run the same code while using xlsread, it works fine.
@Giggs B. Hmm, well I'm not sure then. In the code in my answer, if I change xlsread to readmatrix, it works fine.
This sounds like a fairly simple debugging problem. So, for you, it works with xlsread, but it doesn't work with readmatrix. So, try to use both:
n1 = xlsread('3.csv');
n2 = readmatrix('3.csv');
and have a look in n1 and n2 and see what the difference is. That's a good starting point.
Oh I see, yes then that looks some problem on my end. I will try to figure it out, starting from where you suggested. Thanks!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!