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?
Related
I'm recording live audio in 5 second clips with Python and want to cut out all sound below a certain frequency e.g. 10kHz. This is my script so far:
import pyaudio, wave, time, sys, os
from array import array
from scipy import signal
FORMAT=pyaudio.paInt16
CHANNELS=1
CHUNK=1024
RATE=44100
RECORD_SECONDS=5
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = signal.filtfilt(b, a, data)
return y
audio=pyaudio.PyAudio()
stream=audio.open(format=FORMAT,channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while True:
# read data
data=stream.read(CHUNK)
data_chunk=array('h',data)
data_chunk = butter_highpass_filter(data_chunk,10000,RATE)
frames=[]
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# write to file
words = ["RECORDING-", time.strftime("%Y%m%d-%H%M%S"), ".wav"]
FILE_NAME= "".join(words)
wavfile=wave.open(FILE_NAME,'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))
wavfile.close()
But this doesn't seem to work. I want to cut out all (or as much as possible of the) sound below the specified frequency. Why doesn't the filter I'm using seem to cut out the sound below 10kHz? How can I make it work?
Brief
The goal is to apply a brick-wall 10 kHz high-pass filter to audio, then save it. Audio is recorded continuously and saved in 5 second snippets to separate .wav files.
What we have so far
At the moment the current script:
declares a function to apply a butterworth high-pass filter (butter_high-pass_filter) whose output is an array of floating point values
butter_high-pass_filter uses signal.filtfilt
the input to the function is in short format (bug 1)
data_chunk=array('h',data)
data_chunk = butter_high-pass_filter(data_chunk,10000,RATE)
data_chunk is never used, so a high passed frame of audio is never saved to file (bug 2)
data is read for 5 seconds worth audio
frames=[]
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.read is blocking, so this will wait until the correct amount of audio has been read.
collected data is then written to a wav file in the same format
words = ["RECORDING-", time.strftime("%Y%m%d-%H%M%S"), ".wav"]
FILE_NAME= "".join(words)
wavfile=wave.open(FILE_NAME,'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))
wavfile.close()
Solution
The problem here is that the solutions is multifaceted and requires multiple parts that are currently missing from the current script.
Also, to avoid further complications a slightly different approach needs to be taken than the one originally intended. Rather than applying a filter in real-time a filter can simply be applied to the wav sample data before it is saved.
This
Removes the need for dealing with filter state continuity
limits the need for casting back and forth between data types
Also, the outer forever while loop has been removed. Timing and memory start to become an issue. The program can simply be re-run over and over until there is more clarity on the use case covering
Why must the audio be high pass filtered?
Why can't the filtering take place after all data is recorded (i.e. applied to wav files?
Why does data have to be saved?
Are there limitations on data format? bit-depth? sampling rate?
Until those are answered there are too many possible routes each with limitation that can realistically be covered in a single answer.
Breakdown
Full breakdown for the process will be
Declare a function that takes floating point sample data as input and high pass filters with floating point data as ouput
concatenate 5 seconds of byte-string data from pyaudio into a single variable
unpack data as a 16-bit (signed short) format array of samples
scale samples to floating point format between 1.0 and -1.0
send data to high pass filter
scale filter samples in the range of 16-bit (signed short) format
pack 16-bit filtered samples into a byte string
write filtered byte-string data to a wav file.
Script
import pyaudio, wave, time, sys, os, struct
from array import array
from scipy import signal
# 1. Declare a function that takes floating point sample data as input and high pass
# filters with floating point data as output
# 2. concatenate 5 seconds of byte-string data from PyAudio into a single variable
# 3. unpack data as a 16-bit (signed short) format array of samples
# 4. scale samples to floating point format between 1.0 and -1.0
# 5. send data to high pass filter
# 6. scale filter samples in the range of 16-bit (signed short) format
# 7. pack 16-bit filtered samples into a byte string
# 8. write filtered byte-string data to a wav file.
# ---------------------------------------------------------------------
# 1. Declare High Pass Filter
def butter_highpass(cutoff: float, fs: float, order: int = 5) -> tuple:
"""
Generate FIR and IIR coefficients for a butterworth highpass filter
:param cutoff: cutoff frequency (hz)
:param fs: sampling rate
:param order: filter order
:return: tuple of filter coefficients
"""
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data: [float], cutoff: float, fs: float, order: int = 5) -> [float]:
"""
apply a butterworth high pass filter to sample data in floating point format
:param data: float sample data array
:param cutoff: filter cutoff (hz)
:param fs: sample data sampling rate
:param order: filter order
:return: floating point array of filtered sample data
"""
b, a = butter_highpass(cutoff, fs, order=order)
y = signal.filtfilt(b, a, data)
return y
# ---------------------------------------------------------------------
# Init Global Variables
FORMAT = pyaudio.paInt16
CHANNELS = 1
CHUNK = 1024
RATE = 44100
RECORD_SECONDS = 5
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
# ---------------------------------------------------------------------
# Main Program
if __name__ == '__main__':
# ---------------------------------------------------------------------
# 2. concat 5 seconds of data into a single string
frames = b''
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
frames += stream.read(CHUNK)
# ---------------------------------------------------------------------
# 3. Unpack data as a 16 - bit
sample_data = array('h', frames)
# ---------------------------------------------------------------------
# 4. scale samples to floating point format between 1.0 and -1.0
samples = [sample_datum / (2**15) for sample_datum in sample_data]
print("Max Amplitude:", max(samples))
# ---------------------------------------------------------------------
# 5. send data to high pass filter
filtered_sample_data = butter_highpass_filter(samples, 10000.0, RATE)
# filtered_sample_data = samples
# ---------------------------------------------------------------------
# 6. scale filter samples in the range of 16-bit (signed short) format
# (2 ** 14) for headroom (very lazy)
sample_data_16_bit = [int(sample * (2 ** 14)) for sample in filtered_sample_data]
# # ---------------------------------------------------------------------
# # 7. pack 16-bit filtered samples into a byte string
raw_data = [struct.pack('h', sample) for sample in sample_data_16_bit]
# # ---------------------------------------------------------------------
# # 8. Write Wav
file_name = "".join(["RECORDING-", time.strftime("%Y%m%d-%H%M%S"), ".wav"])
wavfile = wave.open(file_name, 'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(raw_data))
wavfile.close()
Comments
Potential additions could include plotting the spectrum with matplotlib, normalising the filtered audio, encapsulating the process into custom functions, but these have been left as an exercise for the OP.
Below is included a screen shot of the audio spectrum after normalisation. As has been referenced in comments by #marco, the filter does have a slope, which is expected. This can be improved by increasing the order of the filter.
I am new to Python so please pardon me if this question is very basic.
I have Accelerometer Vector Magnitude (acc_VM) signal with sampling frequency of 100Hz. I have to find the Fourier transform of this signal and find the fundamental frequency between range Df.
Df is the family of frequencies corresponding to walking. Here we use Df = [1.2, 4]Hz. How can I choose the frequency range Df = [1.2, 4]Hz using python should I implement filters OR is combFunction() the correct code ?
def combFunction(n):
combSignal = []
for element in n:
if element>1.2 and element<4 :
combSignal.append(element)
else:
combSignal.append(0)
return np.maximum(combSignal)
def hann(total_data):
hann_array = np.zeros(total_data)
for i in range(total_data):
hann_array[i] = 0.5 - 0.5 * np.cos((2 * np.pi * i)/(total_data - 1))
return hann_array
def calculate_FT(x):
hann_weight = hann(len(x))
x_multiplied_hann = x * hann_weight
X = np.abs(np.fft.rfft(x_multiplied_hann))
combSignal = combFunction(X)
calculate_FT(acc_VM)
The FFT does not return frequencies, but rather an array of amplitudes for a fixed set of evenly spaced frequencies.
As a result your combFunction, as implemented, would pick the components which have a spectrum amplitude between 1.2 and 4.
To be able to select frequencies, you would need the corresponding array of those evenly spaced frequencies, which you can get
from np.fft.rfftfreq.
Note that you will need the sampling rate (and if your data isn't uniformly sampled, you will need to resample it).
In the code that follows I'll use the variable sampling_rate for that. Then the frequencies will be given by:
freqs = np.fft.rfftfreq(len(data), sampling_rate)
Now let's extract the array indices corresponding to those frequencies that are within the frequency band of interest:
in_band = np.where([f >= 1.2 and f <= 4 for f in freqs])[0]
Then you may get the location within this band where the original spectrum X has a peak:
peak_location = np.argmax(X[in_band])
which gives you a peak spectrum amplitude X[in_band[peak_location]] at a frequency f[in_band[peak_location]].
Putting it all together should give you something like the following:
def find_peak_in_frequency_range(X, freqs, fmin, fmax):
in_band = np.where([f >= fmin and f <= fmax for f in freqs])[0]
peak_location = np.argmax(X[in_band])
return f[in_band[peak_location]], X[in_band[peak_location]]
def calculate_FT(x, sampling_rage):
hann_weight = hann(len(x))
x_multiplied_hann = x * hann_weight
X = np.abs(np.fft.rfft(x_multiplied_hann))
freqs = np.fft.rfftfreq(len(x), sampling_rate)
peakFreq,peakAmp = find_peak_in_frequency_range(X, freqs, 1.2, 4)
Note that you may get better results by using a spectrum estimation method such as scipy.signal.welch instead of simply taking the FFT.
For sake of illustration, I've ran the above on a sample data set (file 1.csv with some resampling):
I have python 3.4.
I transmitted a 2MHz (for example) frequency and received the cavitation over the time (until I stopped the measurement).
I want to get a spectrogram (cavitation vs frequency) and more interesting is a spectrogram of cavitation over the time of the sub-harmonic (1MHz) frequency.
The data is saved in sdataA (=cavitation), and t (=measurement time)
I tried to save fft in FFTA
FFTA = np.array([])
FFTA = np.fft.fft(dataA)
FFTA = np.append(FFTA, dataA)
I got real and complex numbers
Then I took only half (from 0 to 1MHz) and save the real and complex data.
nA = int(len(FFTA)/2)
yAre = FFTA[range(nA)].real
yAim = FFTA[range(nA)].imag
I tried to get the frequencies by:
FFTAfreqs = np.fft.fftfreq(len(yAre))
But it is totally wrong (I printed the data by print (FFTAfreqs))
I also plotted the data and again it's wrong:
plt.plot(t, FFTA[range(n)].real, 'b-', t, FFTA[range(n)].imag, 'r--')
plt.legend(('real', 'imaginary'))
plt.show()
How can I output a spectrogram of cavitation over the time of the sub-harmonic (1MHz) frequency?
EDIT:
Data example:
see a sample of 'dataA' and 'time':
dataA = [6.08E-04,2.78E-04,3.64E-04,3.64E-04,4.37E-04,4.09E-04,4.49E-04,4.09E-04,3.52E-04,3.24E-04,3.92E-04,3.24E-04,2.67E-04,3.24E-04,2.95E-04,2.95E-04,4.94E-04,4.09E-04,3.64E-04,3.07E-04]
time = [0.00E+00,4.96E-07,9.92E-07,1.49E-06,1.98E-06,2.48E-06,2.98E-06,3.47E-06,3.97E-06,4.46E-06,4.96E-06,5.46E-06,5.95E-06,6.45E-06,6.94E-06,7.44E-06,7.94E-06,8.43E-06,8.93E-06,9.42E-06]
EDIT II:
From #Martin example I tried the following code, please let me know if I did it right.
In the case that dataA and Time are saved as h5 files (or the data that I posted already)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dfdata = pd.read_hdf("C:\\data_python\\DataA.h5")
dft = pd.read_hdf("C:\\data_python\\time.h5")
dft_cor = int((len(dft)-2)*4.96E-6) # calculating the measured time
fs = 2000000 #sampling frequency 2MHz
CHUNK = 10000
signal_time = dft_cor # seconds
def sine(freq,fs,secs):
data=dfdata
wave = np.sin(freq*2*np.pi*data)
return wave
a1 = sine(fs,fs,120)
a2 = sine(fs/2,fs,120)
signal = a1+a2
afft = np.abs(np.fft.fft(signal[0:CHUNK]))
freqs = np.linspace(0,fs,CHUNK)[0:int(fs/2)]
spectrogram_chunk = freqs/np.amax(freqs*1.0)
# Plot spectral analysis
plt.plot(freqs[0:1000000],afft[0:1000000]) # 0-1MHz
plt.show()
number_of_chunks = 1000
# Empty spectrogram
Spectrogram = np.zeros(shape = [CHUNK,number_of_chunks])
for i in range(number_of_chunks):
afft = np.abs(np.fft.fft(signal[i*CHUNK:(1+i)*CHUNK]))
freqs = np.linspace(0,fs,CHUNK)[0:int(fs/2)]
spectrogram_chunk = afft/np.amax(afft*1.0)
try:
Spectrogram[:,i]=spectrogram_chunk
except:
break
import cv2
Spectrogram = Spectrogram[0:1000000,:]
cv2.imshow('spectrogram',np.uint8(255*Spectrogram/np.amax(Spectrogram)))
cv2.waitKey()
cv2.destroyAllWindows()
It seems your problem is not in Python but in understanding what is Spectrogram.
Spectrogram is sequences of spectral analysis of a signal.
1) You need to cut your signal in CHUNKS.
2) Do spectral analysis of these CHUNKS and stick it together.
Example:
You have 1 second of audio recoding (44100 HZ sampling). That means the recording will have 1s * 44100 -> 44100 samples. You define CHUNK size = 1024 (for example).
For each chunk you will do FFT, and stick it together into 2D matrix (X axis - FFT of the CHUNK, Y axis - CHUNK number,). 44100 samples / CHUNK ~ 44 FFTs, each of the FFT covers 1024/44100~0.023 seconds of the signal
The bigger the CHUNK, the more accurate Spectrogram is, but less 'realtime'.
The smaller the CHUNK is, the less acurate is the Spectrogram, but you have more measurements as you measure frequencies 'more often'.
If you need 1MHZ - actually you cannot use anything higher than 1MHZ, you just take half of the resulting FFT array - and it doesnt matter which half, because 1MHZ is just the half of your sampling frequency, and the FFT is mirroring anything that is higher than 1/2 of sampling frequency.
About FFT, you dont want complex numbers. You want to do
FFT = np.abs(FFT) # Edit - I just noticed you use '.real', but I will keep it here
because you want real numbers.
Preparation for Spectrogram - example of Spectrogram
Audio Signal with 150HZ wave and 300HZ Wave
import numpy as np
import matplotlib.pyplot as plt
fs = 44100#sampling frequency
CHUNK = 10000
signal_time = 20 # seconds
def sine(freq,fs,secs):
data=np.arange(fs*secs)/(fs*1.0)
wave = np.sin(freq*2*np.pi*data)
return wave
a1 = sine(150,fs,120)
a2 = sine(300,fs,120)
signal = a1+a2
afft = np.abs(np.fft.fft(signal[0:CHUNK]))
freqs = np.linspace(0,fs,CHUNK)[0:int(fs/2)]
spectrogram_chunk = freqs/np.amax(freqs*1.0)
# Plot spectral analysis
plt.plot(freqs[0:250],afft[0:250])
plt.show()
number_of_chunks = 1000
# Empty spectrogram
Spectrogram = np.zeros(shape = [CHUNK,number_of_chunks])
for i in range(number_of_chunks):
afft = np.abs(np.fft.fft(signal[i*CHUNK:(1+i)*CHUNK]))
freqs = np.linspace(0,fs,CHUNK)[0:int(fs/2)]
#plt.plot(spectrogram_chunk[0:250],afft[0:250])
#plt.show()
spectrogram_chunk = afft/np.amax(afft*1.0)
#print(signal[i*CHUNK:(1+i)*CHUNK].shape)
try:
Spectrogram[:,i]=spectrogram_chunk
except:
break
import cv2
Spectrogram = Spectrogram[0:250,:]
cv2.imshow('spectrogram',np.uint8(255*Spectrogram/np.amax(Spectrogram)))
cv2.waitKey()
cv2.destroyAllWindows()
Spectral analysis of single CHUNK
Spectrogram
I'm working with a large amount of data that comes in daily at 32hz. We want to filter the data to .5hz(Edit: Question originally specified 1hz, changed based on suggestion) as well as down sample it to 1hz. I am using signal.resample for down sampling and signal.filtfilt with a signal.butter filter. However after performing both of these the FFT shows the signal fall off around 0.16hz.
Does the order that you filter than downsample matter? Is it something to do with the procedures? Am I not understanding the methods right?
I included the data that I think is relevant
desiredFreq = 1 * 60 *60 *26
# untouched data
quickPlots(x,32) # 32 hz
# filtered
x = ft.butterLowPass(x, .5, 32)
quickPlots(x,32) # 32 hz
# resampled
x = ft.resampleData(x, desiredFreq)
quickPlots(x,1) # should be 1 hz
def butterLowPass(data,desired,original,order = 5):
""" Creates the coefficients for a low pass butterworth
filter. That can change data from its original sample rate
to a desired sample rate
Args:
----
data (np.array): data to be filtered
desirerd (int): the cutoff point for data in hz
original (int): the initial sampling rate in hz
Returns:
-------
np.array: of the data after it has been filtered
Note:
----
I find that the filter will make the data all 0's if the order
is too high. Not sure if this is my end or scipy.signal. So work with
CAUTION
References:
----------
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.butter.html
https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.signal.filtfilt.html
https://en.wikipedia.org/wiki/Butterworth_filter
https://en.wikipedia.org/wiki/Low-pass_filter
https://en.wikipedia.org/wiki/Nyquist_rate
"""
nyq = .5 * original
cutoff = desired / nyq
b, a = signal.butter(order, cutoff, btype = 'lowpass', analog = False)
return signal.filtfilt(b, a, data, padlen =10)
def resampleData(data, desired):
""" Takes in a set of data and resamples the data at the
desired frequency.
Args:
----
data (np.array): data to be operated on
desired (int): the desired sampled points
over the dataset
Returns:
-------
np.array: of the resamples data
Notes:
-----
Since signal.resample works MUCH faster when the len of
data is a power of 2, we pad the data at the end with null values
and then discard them after.
"""
nOld = len(data)
thispow2 = np.floor(np.log2(nOld))
nextpow2 = np.power(2, thispow2+1)
data = np.lib.pad(
data,
(0,int(nextpow2-nOld)),
mode='constant',
constant_values = (0,0)
)
nNew = len(data)
resample = int(np.ceil(desired*nNew/nOld))
data = signal.resample(data, resample)
data = data[:desired]
return data
def quickPlots(data,fs):
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import signal
import time
fft,pxx=signal.welch(data,fs)
plt.plot(data)
plt.show()
plt.loglog(fft,pxx)
plt.show()
Pictures of FFT:
Raw data(4hz spike due to other frequencies leaking in):
After filtfilt:
After resample:
Edit: After adjusting to filter at .5hz the same problem appears. I am now wondering if it is a problem with how I am displaying my FFT. I've included the quick plotting I was using to display the graphs.
I have a Audio Signal and have imported using wave.open function.
then I am converting the signal into frames and then using a window to take a set number of samples and checking their amplitude to see if it falls within a set threshold. If it does then I consider it as a Silence and if not then and Audio Signal.
import wave
import struct
import numpy as np
import matplotlib.pyplot as plt
sound_file = wave.open('Audio_1.wav', 'r')
file_length = sound_file.getnframes()
sound = np.zeros(file_length)
for i in range(file_length):
data = sound_file.readframes(1)
data = struct.unpack("<h", data)
sound[i] = int(data[0])
sound = np.divide(sound, float(2**15)) # Normalized data range [-1 to 1]
#print sound #vector corresponding to audio signal containing audio samples
Ap = np.pad(sound, (0,int(np.ceil(len(sound) / 2205.)) * 2205 - len(sound)), 'constant', constant_values=0) # Padding of the sound samples so that the input is a multiple of 2205(window Length)
Apr = Ap.reshape((len(Ap) // 2205, 2205))
Apr.shape
array1=(Apr ** 2).sum(axis=1) #Record Sum of Squares of the amplitude of the signal falling within that window
print array1
#print len(array1)
threshold =1103.4
result= np.array(filter(lambda x: x>= threshold, array1)) #filtering elements below set threshold
print result
print len(result)
print np.where(array1>1103.4) # finding starting index of the elements.
Below are my doubts:
How to find the ending index of the window? So that I can specifically slice that window out from the input.
How should I proceed so that I can get back the samples which contain the audio signal and convert those signals into frequency domain using np.fft.fft().
If any statement or question unclear. Please Specify.
Thank you