Please help! I am trying to make a convolution but i am receiving a syntax error. If anyone can help with how to do the convolution it would be much appreciated!
This first part is the main program.
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()
rightCh = rightCh.reshape(len(rightCh ), 1,order='F').copy()
##--------------------------------------------------------------------
outWave = hstack((leftCh, rightCh))/norml*32768.
# Since we'll write outWave to the output wav file which has 16bits short int as the data,
#we should map the values in outWave to the range of 16bits short int by multiplying 32768.
#pl.plot(outWave)
#pl.show()
##--------------------------------------------------------------------
f1 = wave.open("testOutput.wav",'wb')
f1.setnchannels(2)
f1.setsampwidth(2) # width = 2 Bytes
f1.setframerate(framerate)
f1.setnframes(len_L)
f1.writeframes(outWave.astype(np.short).tostring())
# The outWave is float64 type after the convolution, thus we should first translate outWave
# into 16bits int using astype(np.short) and then write it as a string to the target file.
f1.close()
##--------------------------------------------------------------------
print "Finished!"
This part is where i write the convolution function.
import numpy as np
import numpy as np
from numpy import *
import scipy as Sci
import scipy.linalg
import speech.wav
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
y = signal.convolve(x,h)
############################################################################
# Data processing: convolution (TO BE COMPLETED BY STUDENTS)
############################################################################
# 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
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Please help! I am trying to make a convolution but i am receiving a syntax error....
(b) Perform convolution to obtain the discreet if input x[n] = [1 3 2 1] and impulse response, h[n] signal output of y[n], [1 -4 2]. [3 marks] (c) An analogue signal is sampled every 50ms for a duration of 1000 seconds. i) Calculate how much data (samples) are collected. [2 marks] If a Discrete Fourier Transform (DFT) is performed what is the maximum frequency information that can be obtained? [2 marks] Calculate the minimum frequency (the frequency resolution) a...
Hi. It's a python and I got an error below comment import numpy as np arr=np.genfromtxt("/Volumes/Samsung SSD 860 EVO 500GB Media/Download/primenumbers.txt", dtype=int) arr=arr.reshape(-1,1) arr.shape def find_cat(x): if x<= 300: return '<=300' elif x <= 600: return '<=600' else: return '<=1000' arr2 = np.apply_along_axis(find_cat, axis=1, arr=arr) arr2 = arr2.reshape(-1,1) arr3 = np.hstack((arr, arr2)) arr_300 = np.array((col[0] for col in arr3 if col[i]=='<=300'), dtype=int) arr_300 arr_300.shape count_300=len(arr_300) count_300 avg_300=round(np.mean(arr_300, 2) avg_300 print("Number of items in category \ "<=300\"= (one), and average of...
Just the program code please, thank you
Question: How to compute the convolution of these two signals in MatLa.. (1 bookmark) How to compute the convolution of these two signals in MatLab, without using the conv function/command System response: y(t)= 2tu(t)-3(t-1)u(t-1)-(t2)u(t-2) should be this one according to the book's solutions. Suppose that the system of Figure P3.2(a) has the input x(t) given, in Figure P3.2(b). The impulse response is the unit step fund ion h(t)u(t). Find and sketch the system...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
This is my current output for my program.
I am trying to get the output to look like
This is my program
Student.java
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class Student extends javax.swing.JFrame {
BufferedWriter outWriter;
StudentA s[];
public Student() {
StudentGUI();
}
private void StudentGUI() {
jScrollPane3 = new
javax.swing.JScrollPane();
inputFileChooser = new
javax.swing.JButton();
outputFileChooser = new
javax.swing.JButton();
sortFirtsName = new...
Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...
Question 2: Consider the following DT signal: g[n] = 2 "u[-n – 3] * a[n+3], a) Find the convolution sum in the time domain (show all the necessary steps). b) Consider an LTI system with an input of x[n] and impulse response of h[n] as given below: 2, if n = -1 1, if n=0 x[n] = 3-2, if n=1 3, if n=2 -4, if n=3 3, if n = -1 1, if n=0,2 h[n] = 2, if n =...
I need help with this MATLAB exercise.
The given system is y[n] - (3/10)y[n-1] - (1/10)y[n-2] =
2x[n]
The input x[n] is 2cos(2*pi*n/6)(u[n] - u[n-10])
Don't have to answer part 2 of the question.
Zero-state response of a system can be found by using convolution of the input signal and unit impulse response: Use conv command from MATLAB to compute the zero-state response of the system defined in part B to the input x[n] in part C. 1. 2. 3....
Please do h) and i). The answers have to match (show 3 or 4
answers for each to show that they match I do not need 50).
+ a D b 1. Consider the signal flow diagram representing a r/n)- y[n] discrete-time LTI system as shown, with a = 0.9 and b= -0.2. Assume initial rest conditions. In each case, explain your work. D a. (1 mark) Show that the difference equation for this system is y[n] – a •y[n...