A1_Part1


In [27]:
import sys
import os
sys.path.append('../../software/models/')
from utilFunctions import wavread

In [28]:
"""
A1-Part-1: Reading an audio file

Write a function that reads an audio file and returns 10 consecutive samples of the file starting from 
the 50001th sample. This means that the output should exactly contain the 50001th sample to the 50010th 
sample (10 samples). 

The input to the function is the file name (including the path) and the output should be a numpy array 
containing 10 samples.

If you use the wavread function from the utilFunctions module the input samples will be automatically 
converted to floating point numbers with a range from -1 to 1, which is what we want. 

Remember that in python, the index of the first sample of an array is 0 and not 1.

If you run your code using piano.wav as the input, the function should return the following 10 samples:  
array([-0.06213569, -0.04541154, -0.02734458, -0.0093997 ,  0.00769066,	0.02319407,  0.03503525, 
0.04309214, 0.04626606,  0.0441908], dtype=float32)
"""

def readAudio(inputFile):
    """
    Input:
        inputFile: the path to the wav file      
    Output:
        The function should return a numpy array that contains 10 samples of the audio.
    """
    ## Your code here
    # Read the file
    Fs, data = wavread(inputFile)
    
    #extract subset
    N = 50000
    x = data[N:N+10]
    
    return x

In [29]:
print(readAudio('/home/pratyush/workspace/python/audio/sms-tools/sounds/piano.wav'))


[-0.06213569 -0.04541154 -0.02734458 -0.0093997   0.00769066  0.02319407
  0.03503525  0.04309214  0.04626606  0.0441908 ]

A1_Part2


In [30]:
import sys
import os
sys.path.append('../../software/models/')
from utilFunctions import wavread
import numpy as np

"""
A1-Part-2: Basic operations with audio

Write a function that reads an audio file and returns the minimum and the maximum values of the audio 
samples in that file. 

The input to the function is the wav file name (including the path) and the output should be two floating 
point values returned as a tuple.

If you run your code using oboe-A4.wav as the input, the function should return the following output:  
(-0.83486432, 0.56501967)
"""
def minMaxAudio(inputFile):
    """
    Input:
        inputFile: file name of the wav file (including path)
    Output:
        A tuple of the minimum and the maximum value of the audio samples, like: (min_val, max_val)
    """
    ## Your code here
    
    # Read the file
    Fs, data = wavread( inputFile )
    
    #find min and max
    max_val = np.max( data )
    min_val = np.min( data )
    
    return (min_val, max_val)

In [31]:
minMaxAudio('/home/pratyush/workspace/python/audio/sms-tools/sounds/oboe-A4.wav')


Out[31]:
(-0.83486432, 0.56501967)

A1_Part3


In [34]:
import numpy as np

"""
A1-Part-3: Python array indexing

Write a function that given a numpy array x, returns every Mth element in x, starting from the 
first element.  

The input arguments to this function are a numpy array x and a positive integer M such that M < number of 
elements in x. The output of this function should be a numpy array.

If you run your code with x = np.arange(10) and M = 2, the function should return the following output: 
array([0, 2, 4, 6, 8]).
"""
def hopSamples(x,M):
    """
    Inputs:
        x: input numpy array
        M: hop size (positive integer)
    Output:
        A numpy array containing every Mth element in x, starting from the first element in x.
    """
    ## Your code here
    idx = np.arange(0,x.size,M)
    x_hopped = x[idx]
    return x_hopped

In [38]:
x = np.arange(10)
M = 2
hopSamples(x,M)


Out[38]:
array([0, 2, 4, 6, 8])