I need to read a wav file with cycle, period by period. I need help in this task.
MATLAB: How to read a period of wav file in matlab
digital signal processing
Related Solutions
elis - the answer you included was to split the wav file into 1 second chunks. You want to do something different and split the data into 5Mb files. In order to do this (and it will give a rough estimate only), you will need to know some information about your wav file: the number of channels, the number of bits per sample, and the sampling frequency (I'm guessing a bit here based on what I've read online). Luckily, you can use audioinfo to get that information. So we'll do this by an example. Let's create a sample wav file using the handel.mat data
>> handelData = load('handel.mat');
which has a sampling frequency of 8192 and 73113 samples (single channel) and is roughly 8.9 seconds long. Let's increase this to ten minutes (by repeating) and write to file
>> tenMinuteData = repmat(handelData.y, 68, 1);>> audiowrite('handel.wav', tenMinuteData, handelData.Fs);
which creates a ~10Mb file. Using audioinfo gives us
>> handelAudioInfo = audioinfo('handel.wav')handelAudioInfo = Filename: '/Users/.../handel.wav' CompressionMethod: 'Uncompressed' NumChannels: 1 SampleRate: 8192 TotalSamples: 4971684 Duration: 606.89501953125 Title: [] Comment: [] Artist: [] BitsPerSample: 16
Now we are going to split this file into roughly 5Mb chunks which means we need to calculate the number of samples that we write to each of these files. If we (safely?) assume that audio data (excluding wav file header and other information) size of the wav file can be estimated by
SampleRate * NumChannels * (BitsPerSample / 8) * Duration
then a 5Mb data size corresponds to
>> numSamplesPer5MbFile = 5000000 / (handelAudioInfo.NumChannels * handelAudioInfo.BitsPerSample/8)numSamplesPer5MbFile = 2500000
which seems valid since there are ~5000000 samples in the ten minute file. So we can try the following
handelAudioInfo = audioinfo('handel.wav');numSamplesPer5MbFile = 5000000 / (handelAudioInfo.NumChannels * handelAudioInfo.BitsPerSample/8);[y,Fs] = audioread('handel.wav');n = length(y);atBlock = 1;for k = 1:numSamplesPer5MbFile:n audioBlock = y(k:min(k + numSamplesPer5MbFile - 1, n), :); audiowrite(sprintf('handel%03d.wav', atBlock), audioBlock, Fs); atBlock = atBlock + 1;end
For me, this created two new files
-rw-r--r-- 1 staff 5000044 9 Apr 13:54 handel001.wav-rw-r--r-- 1 staff 4943412 9 Apr 13:54 handel002.wav
each roughly 5Mb in size.
If this is the output of wavread, then see if you have two channels in y
[y,fs] = wavread(filename);
If y is two-channels, Nx2, then just plot one:
y = y(:,1); dt = 1/fs; t = 0:dt:(length(y)*dt)-dt; plot(t,y); xlabel('Seconds'); ylabel('Amplitude'); figure plot(psd(spectrum.periodogram,y,'Fs',fs,'NFFT',length(y)));
Best Answer