0

In the code, first I'm opening wav file called output_test.wav. I then filter the noise from the signal using fftpack.

Problem: I'm trying to convert the filtered signal i.e. filtered_sig array into wav file properly. Currently when I open TestFiltered.wav I get the error:

The item was encoded into a format not supported: 0xc00d5212

Upon further investigation it seems I'm not filtering noise correctly?

I think the error comes from the last 2 lines:

filteredwrite = np.fft.irfft(filtered_sig, axis=0)
    
wavfile.write('TestFiltered.wav', frame_rate, filteredwrite)

CODE:

import numpy as np

from scipy import fftpack

import pyaudio
import wave

from scipy.io import wavfile

def playback():
    
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = 8
    WAVE_OUTPUT_FILENAME = "output.wav"


    filename = 'output_test.wav'

    # Set chunk size of 1024 samples per data frame
    chunk = 1024  

    # Open the sound file 
    wf = wave.open(filename, 'rb')

    frame_rate = wf.getframerate()


    wf_x = wf.readframes(-1)


    signal = np.frombuffer(wf_x, dtype='int16')

    #print("signalxx", signal)
    
    

    return [signal, frame_rate]




time_step = 0.5

# get the data 
data = playback()

sig = data[0]
frame_rate = data[1]

# Return discrete Fourier transform of real or complex sequence
sig_fft = fftpack.fft(sig) # tranform the sin function

# Get Amplitude ?
Amplitude = np.abs(sig_fft) # np.abs() - calculate absolute value from a complex number a + ib

Power = Amplitude**2  # create a power spectrum by power of 2 of amplitude

# Get the (angle) base spectrum of these transform values i.e. sig_fft 
Angle = np.angle(sig_fft)    # Return the angle of the complex argument

# For each Amplitude and Power (of each element in the array?) - there is will be a corresponding difference in xxx

# This is will return the sampling frequecy or corresponding frequency of each of the (magnitude) i.e. Power
sample_freq = fftpack.fftfreq(sig.size, d=time_step)

print(Amplitude)
print(sample_freq)


# Because we would like to remove the noise we are concerned with peak freqence that contains the peak amplitude
Amp_Freq = np.array([Amplitude, sample_freq])


# Now we try to find the peak amplitude - so we try to extract
Amp_position = Amp_Freq[0,:].argmax()

peak_freq = Amp_Freq[1, Amp_position]   # find the positions of max value position (Amplitude)

# print the position of max Amplitude
print("--", Amp_position)
# print the frequecies of those max amplitude
print(peak_freq)


high_freq_fft = sig_fft.copy()
# assign all the value the corresponding frequecies larger than the peak frequence - assign em 0 - cancel!! in the array (elements) (?)
high_freq_fft[np.abs(sample_freq) > peak_freq] = 0

print("yes:", high_freq_fft)


# Return discrete inverse Fourier transform of real or complex sequence
filtered_sig = fftpack.ifft(high_freq_fft)

# Using Fast Fourier Transform and inverse Fast Fourier Transform we can remove the noise from the frequency domain (that would be otherwise impossible to do in Time Domain) - done.
print("filtered noise: ", filtered_sig)


print("getiing frame rate $$", frame_rate)


filteredwrite =     np.fft.irfft(filtered_sig, axis=0)

print (filteredwrite)

wavfile.write('TestFiltered.wav', frame_rate, filteredwrite)

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shaz
  • 1,443
  • 1
  • 27
  • 67
  • can you upload the wav file, e.g. to https://www.file.io/ ? – Stef Dec 22 '20 at 08:49
  • Sure, testfiltered.wav - incorrect encoded: https://file.io/DXT3eZnqU3Wa – Shaz Dec 22 '20 at 08:54
  • updated new link: https://file.io/IgKh7vA1H6OK (other one got deleted) – Shaz Dec 22 '20 at 08:55
  • hm, it seems to be a problem with your file format that is not supported by the `wave` module. I get the following error `unknown format: 3` (Python 3.8.2). Your code doesn't throw an error with the sample file from https://file-examples.com/index.php/sample-audio-files/sample-wav-download/. – Stef Dec 22 '20 at 09:03
  • ffprobe says `Stream #0:0: Audio: pcm_f64le ([3][0][0][0] / 0x0003), 44100 Hz, 1 channels, dbl, 2822 kb/s` for your file and `Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, 2 channels, s16, 256 kb/s` for the test file from `file-examples` – Stef Dec 22 '20 at 09:06
  • Still not sure what's going on - I've tried using another wav file from online - and still getting the format is not support:https://www2.cs.uic.edu/~i101/SoundFiles/BabyElephantWalk60.wav – Shaz Dec 22 '20 at 09:18
  • Am I not saving testfiltered.wav in the correct file format? – Shaz Dec 22 '20 at 09:19
  • Okay, I tried playing the testfiltered.wav in audacity - i get a scratching sound and then nothing... I think filtered_sig array is loosing data - perhaps – Shaz Dec 22 '20 at 09:22
  • Yes, something in your program logic is not correct, but I understand to little of the signal processing to be able to help you here. To get a readable wav file, write `filteredwrite.astype('float32')` in the last line instead of your float64 filteredwrite, but this of course doesn't addresss the main issue. – Stef Dec 22 '20 at 09:35
  • @Shaz Can you give the stacktrace in the question ? – MSS Dec 22 '20 at 11:58
  • @MSS I'm running above code in python idle. I'm not getting an error/exception thrown. So i'm assume my filtering computation is logically incorrect... above commenter has used ffprobe module? of which don't know about much... :/ – Shaz Dec 22 '20 at 12:05
  • please refer this answer of mine. https://stackoverflow.com/a/61315604/5462372 It might help you... – MSS Dec 22 '20 at 12:07
  • @MSS interestinyl though, I have managed to graph the filter and unfiltered signal (same code): https://stackoverflow.com/questions/65404314/python-audio-fftpack-filter-noise-signal-and-graph-it -- previous question – Shaz Dec 22 '20 at 12:07
  • I think your error is due to `filteredwrite = np.fft.irfft(filtered_sig, axis=0)`. Before calling `irfft` you need to have data in the format given by `rfft` . – MSS Dec 22 '20 at 12:15
  • Would you mind guiding me with a code example of what you mean. Very new to audio processing. This could possible be the answer – Shaz Dec 22 '20 at 12:19

0 Answers0