<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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.
Step1:  Here we load the file and display its waveform, spectrorgram and amplitude spectrum.
Step2:  Here we also create an HTML5 audio widget to hear how it actually sounds.
Step3:  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.
Step4:  Looking at it, there isn't anything too fishy.
Step5:  It doesn't sound too bad either.
<ASSISTANT_TASK:>
Python Code:
import sys
sys.path.append('../python')
from HTKFeat import MFCC_HTK
import numpy as np
%matplotlib inline
import matplotlib.pyplot as P
Explanation: 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.
End of explanation
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)
Explanation: Here we load the file and display its waveform, spectrorgram and amplitude spectrum.
End of explanation
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')
Explanation: Here we also create an HTML5 audio widget to hear how it actually sounds.
End of explanation
from scipy.signal import resample
signal8k=resample(signal,signal.size/2)
Explanation: 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.
End of explanation
draw_signal(signal8k,8000)
Explanation: Looking at it, there isn't anything too fishy.
End of explanation
html5audio(signal8k,8000,'../python-test/file.wav')
Explanation: It doesn't sound too bad either.
End of explanation
<END_TASK>