python – How to plot frequency spectrum graph by reading my wav file with FFT
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile # get the api
fs, data = wavfile.read('output.wav') # load the data
a = data.T[0] # this is a two channel soundtrack, I get the first track
b = [(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
c = fft(b) # calculate fourier transform (complex numbers list)
d = len(c)/2 # you only need half of the fft list (real signal symmetry)
plt.plot(abs(c[:(d-1)]), 'r')
plt.show()
I GOT ERROR
C:\Users\isaco\PycharmProjects\test1\venv\Scripts\python.exe
C:/Users/isaco/PycharmProjects/test1/main.py
Traceback (most recent call last):
File “C:\Users\isaco\PycharmProjects\test1\main.py”, line 9, in
plt.plot(abs(c[:(d-1)]), 'r')
TypeError: slice indices must be integers or None or have an __index__ method
Process finished with exit code 1
2. ON MY OTHER COMPUTER
I tried running this code the same way on my other computer. I got a different error on the other computer.
C:\Users\isaco\PycharmProjects\pythonProject1\venv\Scripts\python.exe
C:/Users/isaco/PycharmProjects/pythonProject1/main.py
Traceback (most recent call last):
File “C:\Users\isaco\PycharmProjects\pythonProject1\main.py”, line 7, in
b = [(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
TypeError: 'numpy.int16' object is not iterable
Process finished with exit code 1
I’m new to Python. Why am I getting two different errors? I am in the learning process. I’m doing a project on Digital Signal Processing.
After reading the output.wav file where I recorded my voice, I need to calculate it with FFT and then plot the frequency spectrum on the graph.
I couldn’t find the source of the error. How can I fix ? Could you help ?
Read more here: Source link