play sound at audio file sample rate in matlab

You can use audioread and sound functions in Matlab to read and play audio files at their original sample rate.

Here's an example code:

main.m
% Read audio file
[y, Fs] = audioread('example.wav');

% Play audio
sound(y, Fs);
82 chars
6 lines

In the code above, y is the audio signal, and Fs is the sample rate of the file. The audioread function returns these two variables. The sound function takes the audio signal and the sample rate as inputs and plays the sound.

Note that you can also modify the playback speed by changing the frequency of the audio signal. This can be done by resampling the audio signal at a different sample rate. Here's an example code:

main.m
% Read audio file
[y, Fs] = audioread('example.wav');

% Define a new sample rate
newFs = 1.5 * Fs;

% Resample audio signal
y_new = resample(y, newFs, Fs);

% Play resampled audio
sound(y_new, newFs);
202 chars
12 lines

In this example, the audio signal is resampled at a new sample rate of 1.5 times the original sample rate. This changes the frequency of the audio signal, effectively changing its playback speed. The resample function performs the resampling. Note that this can introduce artifacts in the signal, especially if the resampled sample rate is not an integer factor of the original sample rate.

related categories

gistlibby LogSnag