I have the following function where I'm trying to do FFT on a audio file and reset the phases and putting it all back together as a new audio file. First I do the FFT on both channels and then normalize the amplitudes. When I'm trying to do the final matrix from which to save the audio file it says:
ValueError: matrix must be 2-dimensional
even though it is 2-dimensional. Also I'm getting this warning:
ComplexWarning: Casting complex values to real discards the imaginary part arr = N.array(data, dtype=dtype, copy=copy)
I'm not sure if my approach to deal with amplitudes and phases is the best anyway so I'd appreciate any tips how to get my code working.
import scipy.fftpack as fft
from scipy.io import wavfile
import numpy as np
from scipy.signal import hanning
import math
def readNormalize(location):
samplerate, data = wavfile.read(location)
leftChan = data.T[0] # first track of audio
rightChan = data.T[1]
length = len(leftChan)
fftLeft = fft.fft(leftChan[0:], length)
fftRight = fft.fft(rightChan[0:], length)
#length is half(positive frequency) of the the fft data, because other half is negative (complex conjugate)
length = int(length/2)
#getting the normalization value
ownSum = 0;
for i in range(0, length):
ownSum += abs(fftLeft[i])
normalizer = 1/ownSum
amplitudesRight = []
phasesRight = []
phasesLeft = []
amplitudesLeft = []
#normalizing and setting the phases
for i in range(0,length):
#LEFT CHAN
amplitudesLeft.append((abs(fftLeft[i])*normalizer))
phasesLeft.append(0)
#RIGHT CHAN
amplitudesRight.append((abs(fftRight[i])*normalizer))
phasesRight.append((math.pi/2))
#TRIED THIS ASWELL BUT CAN'T APPEND LIKE THIS
#fftLeft[i] = (abs(fftLeft[i])*normalizer)
#fftLeft[i][i] = 0
#fftRight[i] = (abs(fftRight[i])*normalizer)
#fftRight[i][i] = math.pi/2
#putting the phases and amps back to complex form(at least trying)
matrixLeft = np.matrix([amplitudesLeft, phasesLeft], dtype=np.complex128)
matrixRight = np.matrix([amplitudesRight, phasesRight], dtype=np.complex128)
#ifft for the complex
ifftLeft = fft.ifft(matrixLeft)
ifftRight = fft.ifft(matrixRight)
#putting all the data back together,
#doesn't work, says that matrix has to be 2-dimensional
outputMatrix = np.matrix([ifftLeft, ifftRight],dtype=np.int16)
wavfile.write('test.wav',samplerate, outputMatrix)
Any ideas? Thanks!