Plotting .wav on MATLAB and converting to array

Hi all,
I am using the following code to plot a .wav file on MATLAB:
[wave,fs]=audioread('file.wav');
t=0:1/fs:(length(wave)-1)/fs;
plot(t,wave);
Is this the correct way to plot it? and are the y-axis in Volts and the x-axis in Seconds? If it is the correct way, why is the amplitude being trimmed/clipping at 1?
Also, how can I convert the .wav file into an array so I can than copy it into a text file and use it in another program?
Any help would be highly appreciated.

Risposte (1)

Geoff Hayes
Geoff Hayes il 2 Nov 2015
John - according to audioread output parameters the samples are normalized between -1 and 1 if the data type is not specified or is of type double. As you have not included a data type when calling audioread then the "clamping" that you are observing is expected.
As for converting the wav file into an array, isn't that what the wave array is?

8 Commenti

Thanks for the reply. Where should I specify the file type please? As for the .wav to array, how can I get an array to a text file?
[wave,fs]=audioread('file.wav', 'native');
then
dlmwrite('file.txt', wave)
or
save('file.txt', 'wave', '-ascii')
or a number of other ways of saving data as text, depending on the format it needs to be in.
The .wav to .txt worked fine, thanks. Now the problem is with the plot. When including 'native', the plot is not being displayed correctly. How can I fix that please? Also, why is the plot showing two line (green and blue)? Isn't a wave file formed from one signal? Thanks in advance.
.wav files are often two channel; that would generate two plot lines.
We need more information about "not being displayed correctly". What are you seeing that you are not expecting?
One thing to keep in mind is that when the data was written into the .wav file, it might have been scaled and translated automatically at that time, such as would happen if you used MATLAB's audio writing routines without specifying 'native'.
Thanks for the reply. When converting the wav file to txt, should I get a similar plot when loading both file types? When I plotted the .wav file using the code below, I got the first attached plot.
[wave,fs]=audioread('test3.wav','native');
t=0:1/fs:(length(wave)-1)/fs;
plot(t,wave);
Then I plotted the .txt file using the code below and I got the second attached plot. I used the code below to convert the code.
dlmwrite('test3.txt', wave);
John - how are you creating the second plot? You have written the wave data to file using dlmwrite, but how are you reading from test3.txt and how are you plotting the data? Ideally the two plots should be similar so look at the data that you are plotting for both.
Sorry I forgot to include the code for plotting the text file. I used to code below:
.
%Load text file
path='C:\Users\John\Desktop\';
fileName='test3.txt';
fname=fullfile(path,fileName);
s=load(fname);
%Plot the signal
figure(1);
plot(s(:,1), s(:,2));
John - since you have written only the wave data to file (which may be both channels?) then your above code of
plot(s(:,1), s(:,2));
is just plotting one channel vs. the other. You probably only need to plot one channel but do so against time like you have done previously. So if you know the sampling frequency fs then you need to do (like before)
s=load(fname);
t=0:1/fs:(length(s)-1)/fs;
plot(t,s(:,1));

Accedi per commentare.

Richiesto:

il 2 Nov 2015

Commentato:

il 4 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by