Resample and convert into PCM

19 views (last 30 days)
João
João on 7 Feb 2019
Answered: João on 8 Feb 2019
Hello,
First of all I'm using rMATLAB2018.
I'm trying to resample an audio file with sampling rate 44100 Hz to 22050 Hz and then convert the last one into a PCM with 16 bits.
The resample was an easy step but I'm struggling to convert to PCM 16 bits...
This is my attempt:
[x, fs] = audioread(a_filename);
% resample:
fsin = fs;
fsout = 22050;
m = lcm(fsin,fsout);
up = m/fsin;
down = m/fsout;
x_22 = resample(x, up, down);
audiowrite([a_filename,'_22050','.wav'], x_22, fsout);
% convert to PCM 16 bit
fid = fopen([a_filename,'_22050','.wav'], 'r'); % Open wav file
wF = fread (fid, inf, 'int16');% 16-bit signed integer
fwrite(wF,'short');
fclose(fid);
I'm getting this error message:
Error using fwrite
Invalid file identifier. Use fopen to generate a valid file identifier.
What am I doing wrong here? Thank you.

Accepted Answer

João
João on 8 Feb 2019
I get it:
% convert to PCM 16 bit
precision = 'int16';
fidr = fopen([a_filename(1:11), '_22050','.wav'], 'r'); % open .wav file to read
fidw = fopen([a_filename(1:11), '_22050','.pcm'], 'wb'); % open .pcm file to write
w = int16(fread(fidr, inf, precision));% read .wav file
fwrite(fidw, w, precision); % write .pcm file
fclose(fidr);
fclose(fidw);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!