naredil prikaz z FuncAnimate

master
Andrej 2021-02-16 13:55:52 +01:00
parent d6e39e0301
commit 7c82e3bc11
1 changed files with 22 additions and 14 deletions

View File

@ -11,6 +11,7 @@ avg=128 #Število povprečenj spektra
import sounddevice as sd #Poiskati na spletu in naložiti s pip3!
import numpy as np #Uporaba učinkovitih funkcij numpy
import matplotlib.pyplot as plt #Risanje rezultata z matplotlib
import matplotlib.animation as animation
sd.default.device = 0
fskala=np.linspace(0,fs/2,2*pts+1)[:-1] #Izračunaj frekvenčno skalo [Hz]
@ -25,7 +26,6 @@ odziv = np.loadtxt("5mhz-polinom-2048tock.csv", delimiter=',')
a = odziv[:,1]
popravek = np.max(a[4:-1])-a[4:-1]
f=fskala[4:-1]
def spekter(fs,pts,avg,fskala): #Zajem signala z zvočno kartico in povprečenje spektra [dB]
s=sd.rec(pts*(1+avg),samplerate=fs,channels=1) #Vzorčenje ADC zvočne kartice, 2D polje!
@ -40,19 +40,27 @@ def spekter(fs,pts,avg,fskala): #Zajem signala z zvočno kartico in povprečenje
s2=s2+np.square(np.abs(np.fft.rfft(o*s.reshape(-1)[k:k+2*pts],n=4*pts)[:-1]))
k+=pts
s=10*np.log10(nz+0.5*s2/pts/pts/avg) #Povprečje [dB]
print(' Max:',"%.1f"%(np.amax(s)),'dB @',"%.1f"%(fskala[np.argmax(s)]),'Hz')
#print(' Max:',"%.1f"%(np.amax(s)),'dB @',"%.1f"%(fskala[np.argmax(s)]),'Hz')
return s[4:-1]+popravek
wfbp=None
while wfbp!=True: #Prvo risanje spektra z normalizacijo, ponovi s klikom miške
wfbp=plt.waitforbuttonpress(0.01)
while wfbp==None: #Zanka risanja spektra, izhod=pritisk tipke ali miške
s = spekter(fs,pts,avg,fskala)
Pcal = np.amax(s)
K = Pcal - 3 - dSB
fsum = s - K - 3 - 6 - 20*np.log10(np.sin(fskala[4:-1])*np.pi*tau)
plt.plot(f, s, f, fsum)
fig.canvas.draw()
wfbp=plt.waitforbuttonpress(0.01)
plt.close()
### izris z animacijo
f=fskala[4:-1]
initsum = spekter(fs,pts,avg,fskala)
fig,ax = plt.subplots()
raw,pn, = ax.plot(f,initsum,f,initsum)
ax.set_xlim(0, 100000)
ax.set_ylim(-130,-30)
ax.grid(True)
def meritev():
s = spekter(fs,pts,avg,fskala)
Pcal = np.amax(s)
K = Pcal - 3 - dSB
fsum = s - K - 3 - 6 - 20*np.log10(np.sin(f*np.pi*tau))
raw.set_data(f,s)
pn.set_data(f,fsum)
ax.figure.canvas.draw()
return raw,pn
ani = animation.FuncAnimation(fig,meritev,interval=50)
print('*** Konec ***') #pritisk tipke na tipkovnici