Python NumPy - FFT and Reverse FFT?

So, I worked with FFT, and I'm currently trying to get the sound form from a file with FFT (eventually modify it), but then it outputs the modified signal back to the file. I received an FFT sound wave, and then used the inverse FFT function on it, but the output file does not sound correctly. I did not do any filtering according to the waveform - I just test the receipt of frequency data, and then return it back to the file - it should sound the same, but it sounds completely different. Any ideas?

- EDIT -

Since then, I have worked a little on this project, but have not yet received the desired results. The issued sound file is noisy (both louder and additional noise absent from the source file), and sound from one channel leaked to another channel (which was previously quiet). The input audio file is a stereo 2-channel file with sound coming from only one channel. Here is my code:

import scipy import wave import struct import numpy import pylab from scipy.io import wavfile rate, data = wavfile.read('./TriLeftChannel.wav') filtereddata = numpy.fft.rfft(data, axis=0) print (data) filteredwrite = numpy.fft.irfft(filtereddata, axis=0) print (filteredwrite) wavfile.write('TestFiltered.wav', rate, filteredwrite) 

I do not quite understand why this does not work ...?

EDIT: I have archived the problem of the .py file and audio file if this helps solve the problem here .

+6
source share
4 answers
 >>> import numpy as np >>> a = np.vstack([np.ones(11), np.arange(11)]) # We have two channels along axis 0, the signals are along axis 1 >>> a array([[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]]) >>> np.fft.irfft(np.fft.rfft(a, axis=1), axis=1) array([[ 1.1 , 1.1 , 1.1 , 1.1 , 1.1 , 1.1 , 1.1 , 1.1 , 1.1 , 1.1 ], [ 0.55 , 1.01836542, 2.51904294, 3.57565618, 4.86463721, 6.05 , 7.23536279, 8.52434382, 9.58095706, 11.08163458]]) # irfft returns an even number along axis=1, even though a was (2, 11) # When a is even along axis 1, we get a back after the irfft. >>> a = np.vstack([np.ones(10), np.arange(10)]) >>> np.fft.irfft(np.fft.rfft(a, axis=1), axis=1) array([[ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [ 7.10542736e-16, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00, 7.00000000e+00, 8.00000000e+00, 9.00000000e+00]]) # It seems like you signals are along axis 0, here is an example where the signals are on axis 0 >>> a = np.vstack([np.ones(10), np.arange(10)]).T >>> a array([[ 1., 0.], [ 1., 1.], [ 1., 2.], [ 1., 3.], [ 1., 4.], [ 1., 5.], [ 1., 6.], [ 1., 7.], [ 1., 8.], [ 1., 9.]]) >>> np.fft.irfft(np.fft.rfft(a, axis=0), axis=0) array([[ 1.00000000e+00, 7.10542736e-16], [ 1.00000000e+00, 1.00000000e+00], [ 1.00000000e+00, 2.00000000e+00], [ 1.00000000e+00, 3.00000000e+00], [ 1.00000000e+00, 4.00000000e+00], [ 1.00000000e+00, 5.00000000e+00], [ 1.00000000e+00, 6.00000000e+00], [ 1.00000000e+00, 7.00000000e+00], [ 1.00000000e+00, 8.00000000e+00], [ 1.00000000e+00, 9.00000000e+00]]) 
+4
source
  • It seems you are not applying any filter.
  • You probably want to take ifft fft (post- fft ) rather than the input waveform.
+6
source

Shouldn't it be bigger?

 filtereddata = numpy.fft.fft(data) # do fft stuff to filtereddata filteredwrite = numpy.fft.ifft(filtereddata) wavfile.write('TestFiltered.wav', rate, filteredwrite) 
+4
source

Two problems.

You are using FFTing 2-channel data. You should only have FFT 1 monodata channel for FFT results to make the usual sense. If you want to process 2 channels of stereo data, you must use IFFT (FFT ()) each channel separately.

You use a real fft that discards information and thus makes fft irreversible.

If you want to invert, you will need to use the FFT, which produces a complex result, and then reformat this vector with the extended frequency domain back to the time domain. If you change the vector of the frequency domain, make sure that it remains conjugate symmetric if you want a strictly real result (minus the numerical noise).

+2
source

Source: https://habr.com/ru/post/913603/


All Articles