-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfftExplore.py
More file actions
38 lines (35 loc) · 955 Bytes
/
fftExplore.py
File metadata and controls
38 lines (35 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from scipy.fft import fft, ifft, fftfreq
from pcmExplore import getBytesFromTrack,getFloatsfromTrack,generateSamplePCM
import matplotlib.pyplot as plt
from struct import *
import numpy as np
#assumes data is stored as 16 bit encoding little endian short
def plotTimeAndFreqDomainOfAudio(sample_rate,file_path):
raw_bytes=getBytesFromTrack(file_path)[0]
file=open(file_path)
l=len(raw_bytes)
a=0
b=0
samples=[]
for i in range(l):
b+=2
try:
samples.append(unpack('<h',raw_bytes[a:b])[0])
except error:
pass
a+=2
N=len(samples)
freq_res=sample_rate/N
print("amount of samples: ",N)
print("sample rate: ", sample_rate)
print("frequency resolution: ",freq_res)
print("min: ",min(samples))
print("max: ",max(samples))
y=fft(samples)
yinv=ifft(y)
x=fftfreq(N,1/sample_rate)
plt.plot(x,np.abs(y))
plt.show()
plt.plot(yinv)
plt.show()
plotTimeAndFreqDomainOfAudio(48000,"creation_test.pcm")