Documentation |
This example shows how to change the sampling rate of a signal. The example has two parts. Part one changes the sampling rate of a sinusoidal input from 44.1 kHz to 48 kHz. This workflow is common in audio processing. The sampling rate used on compact discs is 44.1 kHz, while the sampling rate used on digital audio tape is 48 kHz. Part two changes the sampling rate of a recorded speech sample from 7418 Hz to 8192 Hz.
Create an input signal consisting of a sum of sine waves sampled at 44.1 kHz. The sine waves have frequencies of 2, 4, and 8 kHz.
Fs = 44.1e3; t = 0:1/Fs:1-(1/Fs); x = cos(2*pi*2000*t)+1/2*sin(2*pi*4000*(t-pi/4))+1/4*cos(2*pi*8000*t);
To change the sampling rate from 44.1 to 48 kHz, you have to determine a rational number (ratio of integers), P/Q, such that P/Q times the original sampling rate, 44100, is equal to 48000 within some specified tolerance.
To determine these factors, use rat. Input the ratio of the new sampling rate, 48000, to the original sampling rate, 44100.
[P,Q] = rat(48e3/Fs); abs(P/Q*Fs-48000)
You see that P/Q*Fs only differs from the desired sampling rate, 48000, on the order of 10^{–12}.
Use the numerator and denominator factors obtained with rat as inputs to resample to output a waveform sampled at 48 kHz.
xnew = resample(x,P,Q);
If your computer can play audio, you can play the two waveforms. Set the volume to a comfortable level before you play the signals. Execute the play commands separately so that you can hear the signal with the two different sampling rates.
P44_1 = audioplayer(x,44100); P48 = audioplayer(xnew,48000); play(P44_1) play(P48)
Change the sampling rate of a speech sample from 7418 Hz to 8192 Hz. The speech signal is a recording of a speaker saying "MATLAB".
Load the speech sample.
load mtlb
Loading the file mtlb.mat brings the speech signal, mtlb, and the sampling rate, Fs, into the MATLAB^{®} workspace.
Determine a rational approximation to the ratio of the new sample rate, 8192, to the original sample rate. Use rat to determine the approximation.
[P,Q] = rat(8192/Fs);
Resample the speech sample at the new sampling rate.
mtlb_new = resample(mtlb,P,Q);
If your computer has audio output capability, you can play the two waveforms at their respective sampling rates for comparison. Set the volume on your computer to a comfortable listening level before playing the sounds. Execute the play commands separately to compare the speech samples at the different sampling rates.
Pmtlb = audioplayer(mtlb,Fs); Pmtlb_new = audioplayer(mtlb_new,8192); play(Pmtlb) play(Pmtlb_new)