Как получить среднее значение FFT для каждой полосы частот? - PullRequest
0 голосов
/ 05 июня 2019

Привет всем, я пытаюсь получить среднее значение FFT моих 7 частотных полос Это код до сих пор. Работает с функцией журнала. Проблема в том, что я хочу, чтобы среднее значение fft каждой полосы Как я могу это сделать? Кто-то сказал, чтобы получить более низкие индексы, но я не могу понять это Это становится сложным для меня сейчас. Это код >>

import pyaudio
import numpy as np
import math
np.set_printoptions(suppress=True) # don't use scientific notation

CHUNK = 2048 # number of data points to read at a time
RATE = 44100 # time resolution of the recording device (Hz)


p=pyaudio.PyAudio() # start the PyAudio class
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
              frames_per_buffer=CHUNK) #uses default input device

#maxValue            = 2**16
# 7 Freq Bands

NUM_BAND = 7
lower = 0
d = math.log10(CHUNK/2-1)/NUM_BAND


for i in range(1): #to it a few times just to see
    try:
        data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
        fft = abs(np.fft.fft(data).real)
        fft = fft[:int(len(fft)/2)] # keep only first half

        freq = np.fft.fftfreq(CHUNK,1.0/RATE)
        freq = freq[:int(len(freq)/2)] # keep only first half


        band_lower = []
        for i in range(NUM_BAND):
            higher = round(math.pow(10,(i+1)*d))
            print("Band %d from freq[%d]=%f to freq[%d]=%f"%(i, lower, 
            freq[lower], higher, freq[higher]))
            lower = higher+1

    except KeyboardInterrupt:
        stream.stop_stream()
        stream.close()
        p.terminate()

А это распечатка

Band 0 from freq[0]=0.000000 to freq[3]=64.599609
Band 1 from freq[4]=86.132812 to freq[7]=150.732422
Band 2 from freq[8]=172.265625 to freq[19]=409.130859
Band 3 from freq[20]=430.664062 to freq[52]=1119.726562
Band 4 from freq[53]=1141.259766 to freq[141]=3036.181641
Band 5 from freq[142]=3057.714844 to freq[380]=8182.617188
Band 6 from freq[381]=8204.150391 to freq[1023]=22028.466797

1 Ответ

0 голосов
/ 05 июня 2019

Вы можете вычислить среднее значение, используя np.average:

start и end - ваши группы.

import numpy as np 

a = np.array([[1, 2, 3], [4, 5, 6], [3,4,5]])

print(np.average(a[start:end], axis=0))
...