Learn R Programming

phonTools (version 0.1-6)

resample: Resample a Sound

Description

Resample a sound at a different sampling rate.

Usage

resample (sound, newfs, oldfs)

Arguments

sound
Either a numeric vector representing a sequence of samples taken from a sound wave or a sound object created with the loadsound() or makesound() functions.
newfs
The new desired sampling frequency.
oldfs
The original sampling frequency. If a 'sound' object is provided, this does not need to be specified.

Value

  • The resampled vector is returned. If a 'sound' object another 'sound' object is returned representing the resampled sound.

Details

The sound vector is resampled using interpolation or decimation as appropriate. The new and old sampling frequencies must be relatable by an integer, though any new sampling frequency may be achieved by first upsampling and then downsampling the signal.

Examples

Run this code
data (sound)
## downsample and then upsample the sound back to its original sampling frequency
downsamped = resample (sound, 11025)
upsamped = resample (downsamped, 22050)

## compare the beginning of the waveforms for all three sounds
par (mfrow = c(3,1), mar = c(4,4,1,1))
plot (sound$sound[1:2000], type = 'l')
plot (upsamped$sound[1:2000], type = 'l', col = 2)
plot (downsamped$sound[1:1000], type = 'l', col = 4)

## the original sound, and the sound which was downsampled 
## then upsampled are very highly correlated
cor (sound$sound, upsamped$sound)

Run the code above in your browser using DataLab