MATLAB: How to change the sampling frequency of audio signal

speech recognitionsspeech restorations

Hi,
I am working on a speech recognition , and am aiming to change the sampling frequency of the audio signal. I wrote the following code :
clear y Fs
%Read the data to the MATLAB using audioread.
[y,fs] = audioread(filename);
%Play the audio.
sound(y,fs);
%change the sampling rate
fs2= fs/2;
audiowrite(filename)
%Read the data back into MATLAB using audioread.
[y,fs2] = audioread(filename);
sound(y,fs2);
when I run it I heard the sound twice in different FS together, But the problem is the original sound has been changed because I wrote on it (audiowrite (filename)).
So can some one help me to change the Fs without changing the original one and compare the results by sound instruction and plot instruction .

Best Answer

audiowrite(filename) is going to fail because you must provide at least 3 parameters to audiowrite: filename, samples, and output frequency.
If you do not want to overwrite the original file then supply a different file name. For example,
newfile = tempname();
audiowrite(newfile, y, fs2);
If all you are going to do with it is read it back in again, then it is pointless to do so: you are just going to get y and fs2 back again. audiowrite() does not resample the data: it just writes the frequency in the header, and whatever tool you use to play the sound is responsible for taking care of the frequency. For example,
sound(y, fs);
sound(y, fs/2);
You probably want resample(), or fft() then ifft(), or interp1()