Displaying the Frequency Spectrum Using Pyaudio

Asked 2 years ago, Updated 2 years ago, 20 views

When you try to display the frequency spectrum using pyaudio, the frequency deviated from the sound you made is displayed

# Recording Plot Section
import pyaudio
import numpy as np
import matplotlib.pyplot asplt
from scipy import signal
import wave

# %matplotlib inline

input_device=0
output_device=1

def recording (CHANNELS, RATE, CHUNK, RECORD_SECONDS, FORMAT, p):
    print("Recording")
    stream=p.open(format=FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    input_device_index = input_device,
                    output_device_index=output_device,
                    frames_per_buffer=CHUNK)

    all = [ ]
    for i in range (0, int (RATE/CHUNK* RECORD_SECONDS):
        data=stream.read(CHUNK)
        all.append(data)
    stream.close()
    p.terminate()
    data=b'.join(all)
    print ("Finish")
    return data

if__name__=='__main__':
    CHANNELS = 1
    RATE=44100
    CHUNK = 2**11
    RECORD_SECONDS = 3
    FORMAT=pyaudio.paInt16
    p=pyaudio.PyAudio()
    data=recording (CHANNELS, RATE, CHUNK, RECORD_SECONDS, FORMAT, p)

    x = np.frombuffer(data,dtype="int16")/10000

    xs=(np.ft.ft(np.frombuffer(data,dtype="int16"))))[:int(len(x)/2)]

    # take an absolute value of the amplitude of
    xr = np.abs(xs)

    # peak detection
    maxid=signal.argrelmax(xr,order=100)#large peaks
    minid=signal.argrelmin(xr,order=100)#small peaks

    # graph plot
    plt.figure(figsize=(13,3))
    plt.axis

    plt.plot(maxid[0], xr[maxid[0]], 'rx', label='peak')
    plt.plot(minid[0], xr[minid[0]], 'bx', label='min peak')
    plt.plot(xr, label="beforeBPF")
    plt.xlim(0,7000)#Hz
    plt.legend()
    plt.show()

When I made a 1 kHz sound on the app I usually use in the sound field, it peaked at 3 kHz.
The figure below shows the output at that time.
Graph shown when ringing 1 kHz

python

2022-09-29 22:02

1 Answers

Do you know what the results of FFT show?
int(fs/chunk*sec)=int(44100/2048*3)=64
If you put chunk*64=131072 data in FFT, the frequency resolution will be 44100/131072=0.336 Hz.
The 3000th data is 3000*0.336 and about 1 kHz is correct?


2022-09-29 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.