Resampling from 16kHz to 8kHz

This notebook demonstrates resampling from 16kHz to 8kHz using the scipy.signal.resample method. This isn't theonly or the best method, but it does work.

First we include some of the code that will help load a sample audio file, just like in the other notebook.


In [1]:
import sys
sys.path.append('../python')

from HTKFeat import MFCC_HTK

import numpy as np
%matplotlib inline
import matplotlib.pyplot as P

Here we load the file and display its waveform, spectrorgram and amplitude spectrum.


In [28]:
mfcc=MFCC_HTK()

signal = mfcc.load_raw_signal('../python-test/file.raw')

def draw_signal(signal, fs):
    sig_len=signal.size/fs #in seconds

    P.figure(figsize=(15,4))
    t=np.linspace(0,sig_len,signal.size)
    P.plot(t,signal)
    P.figure(figsize=(15,4))
    s=P.specgram(signal,Fs=fs)
    P.xlim(0,sig_len)
    P.figure(figsize=(15,4))
    f=np.linspace(0,fs/2,signal.size/2+1)
    P.plot(f,np.abs(np.fft.rfft(signal)))
    
draw_signal(signal,16000)


Here we also create an HTML5 audio widget to hear how it actually sounds.


In [11]:
from ipywidgets import *
from IPython.display import display
import scipy.io.wavfile

def html5audio(signal, fs, filepath):

    scipy.io.wavfile.write(filepath,fs,signal.astype('int16'))

    src = "<audio controls=\"controls\" style=\"width:600px\" >\
        <source src=\"{}\" type=\"audio/wav\" />\
        Your browser does not support the audio element.\
        </audio>".format(filepath)
    
    display(HTML(src))

html5audio(signal,16000,'../python-test/file.wav')

Resampling

This is the actuall resampling code. The second argument is simply the number of samples in the destination signal. For 16->8 this is half of the original. For 44.1->8, it's supposed to be signal.size/5.5125.


In [21]:
from scipy.signal import resample

signal8k=resample(signal,signal.size/2)

Looking at it, there isn't anything too fishy.


In [29]:
draw_signal(signal8k,8000)


It doesn't sound too bad either.


In [24]:
html5audio(signal8k,8000,'../python-test/file.wav')

The only downside to this method is that it's slow. Read the documentation for more info: http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.html#scipy.signal.resample