Question
import numpy as np def myconv(x,h): ############################################################################ # A function to generate the output signal y as convolution of input signal # x and impulse
import numpy as np def myconv(x,h):
############################################################################ # A function to generate the output signal y as convolution of input signal # x and impulse response signal h ############################################################################
# INPUT PARAMETERS--------------------------------------------------------- # x: input signal # h: impulse response len_x = len(x) # length of x len_h = len(h) # length of h ############################################################################ # Data processing: convolution (TO BE COMPLETED BY YOU) ############################################################################ # OUTPUT PARAMETERS-------------------------------------------------------- # y: output signal as convolution of input signal x and impulse response h
# Write the code including comments to explain what you are doing
return y
|-----------------------------------------|----------------------------------------------------|------------------------------------------|
import numpy as np def my_diffequation(x, alpha, N):
############################################################################ # A function to generate the output signal y of the system described by a # diffence equation ############################################################################
# INPUT PARAMETERS--------------------------------------------------------- # x: input signal # alpha: decreasing amplitude # N: delay between consecutive echoes
############################################################################ # Data processing (TO BE COMPLETED BY YOU) ############################################################################ # OUTPUT PARAMETERS-------------------------------------------------------- # y: output signal
L=len(x) # Write the code including comments to explain what you are doing
return y
|----------------------------|----------------------------------------------|---------------------------------------------|
import numpy as np from numpy import * import pylab as pl import wave import struct from my_conv import myconv #import scipy.signal as signal ##-------------------------------------------------------------------- ## read the input wave file "speech.wav"
f = wave.open("speech.wav", "rb")
params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4]
str_data = f.readframes(nframes) f.close()
if (framerate!=44100): # if Fs is not equal to 44100 print ('Fs is not 44.1KHz') # display error and abort
waveData = np.fromstring(str_data, dtype=np.short) waveData = waveData/32768.0 #normalization
len_data = len(waveData) #pl.plot(waveData) #pl.show() ##-------------------------------------------------------------------- ## read the impulse response for the system len_imp = 128 # length of left/right impulse response (fixed)
fp = open("H0e090a.dat", 'rb') # open imp_filename sysData=zeros(2*len_imp) for i in range(0,2*len_imp): strd = fp.read(2) data1 = struct.unpack(">h", strd) sysData[i] = int(data1[0])
fp.close() # close imp_filename ## separate the left and right channel impulse response leftimp = sysData[0:2*len_imp-1:2] # left ear impulse response: hl[1]...hl[len_imp] rightimp = sysData[1:2*len_imp:2] # right ear impulse response: hr[1]...hr[len_imp] #pl.plot(rightimp) #pl.show() ##-------------------------------------------------------------------- #leftCh = signal.convolve(waveData, leftimp, mode='valid') #rightCh = signal.convolve(waveData, rightimp, mode='valid') leftCh = myconv(leftimp, waveData) # convolution of left ear impulse response and waveData rightCh = myconv(rightimp, waveData) # convolution of left ear impulse response and waveData len_R= len(rightCh) len_L= len(leftCh) #pl.plot(leftCh) #pl.plot(rightCh) #pl.show() ##-------------------------------------------------------------------- LR_Ch = vstack((leftCh, rightCh)) norml= 1.05*abs(LR_Ch).max() leftCh = leftCh.reshape(len(leftCh ), 1,order='F').copy()
|----------------------------------------------------------|---------------------------------------------------------|----------------------------------------|
import numpy as np from numpy import * import pylab as pl import wave import struct import scipy.signal as signal #from my_diff_equation import my_diffequation ##-------------------------------------------------------------------- # LTI system parameters N = 2250 # delay alpha = 0.9 # amplitude scaling b = np.array([1]) a = zeros(N+1) a[0] = 1 a[N] = -alpha ##-------------------------------------------------------------------- ## read the input wave file "speech.wav" f = wave.open("speech.wav", "rb")
params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4]
str_data = f.readframes(nframes) f.close()
if (framerate!=44100): # if Fs is not equal to 44100 print ("Fs is not 44.1KHz") # display error and abort
waveData = np.fromstring(str_data, dtype=np.short) waveData = waveData/32768.0 #normalization
len_data = len(waveData)
Nf = float(N) strDelay = "The delay of N = %f samples corresponds to a delay %f seconds" %(N, Nf/framerate) print (strDelay) #pl.plot(waveData) #pl.show() ##-------------------------------------------------------------------- # echo is the output from the differential equation echo = my_diffequation(waveData, alpha, N) len_e = len(echo) # echo2 is the output of the IIR filter echo2 = signal.lfilter(b, a, waveData) len_e2 = len(echo2) # If you want to check if the code works fine, you can comment my_diffequation(waveData, alpha, N) # and only use echo2 = signal.lfilter(b, a, waveData) # which uses lfilter function in signal package. # The results echo and echo2 should be the same.
#pl.subplot(211) #pl.plot(echo) #pl.subplot(212) #pl.plot(echo2) #pl.show() ##-------------------------------------------------------------------- norml1= 1.05*abs(echo).max() norml2= 1.05*abs(echo2).max() ##-------------------------------------------------------------------- outWave1 = echo/norml1*32768 outWave2 = echo2/norml2*32768 #pl.plot(outWave) #pl.show() ##-------------------------------------------------------------------- f1 = wave.open("echo.wav",'wb') f1.setnchannels(1) f1.setsampwidth(2) # width = 2 Bytes f1.setframerate(framerate) f1.setnframes(len_e) f1.writeframes(outWave1.astype(np.short).tostring()) f1.close() f2 = wave.open("echo2.wav",'wb') f2.setnchannels(1) f2.setsampwidth(2) # width = 2 Bytes f2.setframerate(framerate) f2.setnframes(len_e2) f2.writeframes(outWave2.astype(np.short).tostring()) f2.close() ##-------------------------------------------------------------------- print ("Finished!")
Given the following files write a Convolution function and a difference function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started