def fft_analysis(groesse, hoehe, nummer): #Daten mit Korrektur dataname = groesse + '_' + hoehe + '_' + nummer data = cassy.CassyDaten('daten/' + dataname + '.lab') time = data.messung(1).datenreihe('t').werte pres = data.messung(1).datenreihe('p_A1').werte #Signale abschneiden time, pres = analyse.untermenge_daten(time, pres, 0, tgrenz[dataname]) fourier = analyse.fourier_fft(time, pres) freq = fourier[0] amp = fourier[1] peak = analyse.peakfinder_schwerpunkt(freq[3:100], amp[3:100]) delta = korrektur(time, pres) return (2 * np.pi * peak, delta)
def test_fourier(): tmin = 100. tmax = 500. tstep = 0.2 freq = 0.123 t = np.arange(tmin, tmax, tstep) y = np.cos(2. * np.pi * freq * t) figure() subplot(2, 1, 1) title('Daten') plot(t, y, '.') xlabel('$t$ / s') ylabel('amp') grid() subplot(2, 1, 2) title('Fourierspektrum') freq, amp = analyse.fourier(t, y) freq_fft, amp_fft = analyse.fourier_fft(t, y) plot(freq, amp, '.', color='blue', label="Fourier") amp_fft = np.max(amp) / np.max(amp_fft) * amp_fft plot(freq_fft, amp_fft, '.', color='red', label="FFT") xlabel('$f$ / Hz') ylabel('amp') grid() legend() fpeak = analyse.peakfinder_schwerpunkt(freq, amp) fpeak_fft = analyse.peakfinder_schwerpunkt(freq_fft, amp_fft) axvline(fpeak, color='blue') axvline(fpeak_fft, color='red') xlim(0., np.max(freq)) ylim(0., max(np.max(amp), np.max(amp_fft)) * 1.1) show()
grid() subplot(2, 1, 2) plot(t, U2) ylabel('$U$ / V') xlabel('t/s') xlim(0, 0.04) title('zweiter Schwingkreis') grid() subplots_adjust(hspace=0.5) savefig('Images/Rohdaten_' + n[0] + 'sinnig.pdf') plt.figure() #FFT + Plot w1_array, A1 = analyse.fourier_fft(t, U1) w1 = analyse.peakfinder_schwerpunkt(w1_array, A1) #Hauptfrequenz1 print("Frequenz 1 aus fft: ", w1) plt.plot(w1_array, A1 / max(A1)) plt.axvline(x=w1, color="darkred", linestyle="--") plt.text(w1, 0.5, 'Max: {} Hz'.format(round(w1, 3))) plt.title('FFT Schwingung 1') plt.xlim(0, 4000) plt.xlabel('Frequenz / Hz') plt.ylabel('rel. Häufigkeiten') plt.savefig(n[0] + 'sinnig_FFT1') plt.figure() w2_array, A2 = analyse.fourier_fft(t, U2) w2 = analyse.peakfinder_schwerpunkt(w2_array, A2) #Hauptfrequenz2
plt.title("Schwebung bei $l_F$=52.7cm") plt.ylabel("$U_1$ / V") plt.xlabel("t / s") plt.plot(t, U1) scatter(peaks1[0], peaks1[1], color="r") subplot(2, 1, 2) plt.ylabel("$U_2$ / V") plt.xlabel("t / s") plt.plot(t, U2) scatter(peaks2[0], peaks2[1], color="r") plt.savefig("images/527_schwe.png") plt.show() plt.close() omega_fft1, A1 = anal.fourier_fft(t, U1) omega_fft2, A2 = anal.fourier_fft(t, U2) plt.title("Fouriertransformierte der Schwebungen bei $l_F$=52.7cm") plt.ylabel("Häufigkeit") plt.xlabel("f / Hz") plt.xlim(0.4, 0.8) plt.plot(omega_fft1, A1) plt.plot(omega_fft2, A2) plt.show() #--------------------------------------------------- #Schwebung bei lF=0.781 m #--------------------------------------------------- t, U1, U2 = c_open("781cm/Schwebung.lab") U1 = np.array(U1) - np.average(U1)
voltage = voltage - offset figure(1) title('Pendel') subplot(2, 1, 1) plot(timeValues, voltage) grid() xlabel('Zeit / s') ylabel('Spannung / V') einhuellende = analyse.exp_einhuellende(timeValues, voltage, voltageError) plot(timeValues, +einhuellende[0] * exp(-einhuellende[2] * timeValues)) plot(timeValues, -einhuellende[0] * exp(-einhuellende[2] * timeValues)) subplot(2, 1, 2) fourier = analyse.fourier_fft(timeValues, voltage) frequency = fourier[0] amplitude = fourier[1] plot(frequency, amplitude) grid() xlabel('Frequenz / Hz') ylabel('Amplitude') maximumIndex = amplitude.argmax() xlim(frequency[max(0, maximumIndex - 10)], frequency[min(maximumIndex + 10, len(frequency))]) peak = analyse.peakfinder_schwerpunkt(frequency, amplitude) axvline(peak) L = 0.667 g = ((2 * np.pi * peak)**2) * L
dis = 0 i = 0 while i < len(ind) - 1: dis = dis + v[ind[i + 1]] - v[ind[i]] i = i + 1 dis = dis / (len(ind) - 1) return dis data = cassy.CassyDaten('Gleichsinnig/gleich.koppl.eisen.lab') U1 = data.messung(1).datenreihe('U_B1').werte U2 = data.messung(1).datenreihe('U_B2').werte t = data.messung(1).datenreihe('t').werte w1, A1 = analyse.fourier_fft(t, U1) w1 = analyse.peakfinder_schwerpunkt(w1, A1) print("Frequenz 1 aus fft: ", w1) w2, A2 = analyse.fourier_fft(t, U2) w2 = analyse.peakfinder_schwerpunkt(w2, A2) print("Frequenz 2 aus fft: ", w2) w_mean = (w1 + w2) / 2 w_std = np.sqrt((w1 - w_mean)**2 + (w2 - w_mean)**2) print("mittlere Frequenz aus fft", w_mean, "+-", w_std) fig = figure() subplot(2, 1, 1) plot(t, U1)
# Daten laden data1 = cassy.CassyDaten('daten/spektrum_1.2.lab') time1 = data1.messung(1).datenreihe('t').werte volt1 = data1.messung(1).datenreihe('U_A1').werte data2 = cassy.CassyDaten('daten/spektrum_2.lab') time2 = data2.messung(1).datenreihe('t').werte volt2 = data2.messung(1).datenreihe('U_A1').werte data3 = cassy.CassyDaten('daten/spektrum_3.1.lab') time3 = data3.messung(1).datenreihe('t').werte volt3 = data3.messung(1).datenreihe('U_A1').werte # Fourier fourier1 = analyse.fourier_fft(time1, volt1) fourier2 = analyse.fourier_fft(time2, volt2) fourier3 = analyse.fourier_fft(time3, volt3) fre1 = fourier1[0] amp1 = fourier1[1] fre2 = fourier2[0] amp2 = fourier2[1] fre3 = fourier3[0] amp3 = fourier3[1] # Neagtive Frequenzen wegschneiden fre1, amp1 = analyse.untermenge_daten(fre1, amp1, 0, 5000) fre2, amp2 = analyse.untermenge_daten(fre2, amp2, 0, 5000) fre3, amp3 = analyse.untermenge_daten(fre3, amp3, 0, 5000)
while i < len(ind)-1: dis = dis + v[ind[i+1]]-v[ind[i]] i = i+1 dis = dis/(len(ind)-1) return dis data = cassy.CassyDaten('Schwebung_1_250n.lab') U1 = data.messung(1).datenreihe('U_B1').werte t = data.messung(1).datenreihe('t').werte ind_max = find_peaks_cwt(U1,np.arange(1,20)) ind_max = ind_max[:20] ind_max = ind_max[3:] w_array,A = analyse.fourier_fft(t,U1) ind_w1=analyse.peak(w_array,A,np.argmax(w_array>950),np.argmax(w_array>1020)) ind_w2=analyse.peak(w_array,A,np.argmax(w_array>1100),np.argmax(w_array>1160)) plot(w_array,A/max(A)) plt.axvline(x=w_array[int(ind_w1)], color="darkred", linestyle = "--") plt.text(w_array[int(ind_w1)],0.7,'max_1:{} Hz'.format(np.round(w_array[int(ind_w1)],4))) plt.axvline(x=w_array[int(ind_w2)], color="darkred", linestyle = "--") plt.text(w_array[int(ind_w2)],0.5,'max_2:{} Hz'.format(np.round(w_array[int(ind_w2)],4))) xlim(0,4000) plt.savefig('Images/Schwebung_FFT') err1=w_array[int(np.ceil(ind_w1))]-w_array[int(np.floor(ind_w1))] err2=w_array[int(np.ceil(ind_w2))]-w_array[int(np.floor(ind_w2))] #Ausgabe der Werte print('f+={} +- {}'.format(w_array[int(ind_w1)],err1)) print('f-={} +- {}'.format(w_array[int(ind_w2)],err2))
ax2.set_ylabel('$U_2$ / V') ax2.set_xlabel('$t$ / ms') ax2.set_xlim(0,10) ax2.grid() plt.subplots_adjust(hspace=0) plt.savefig('plots/fundamental_roh.pdf', format='pdf', dpi=1200) plt.show() ''' # Frequenzspektrum data1 = cassy.CassyDaten('daten/fundamental_3.lab') time1 = data1.messung(1).datenreihe('t').werte vol1_1 = data1.messung(1).datenreihe('U_A1').werte vol1_2 = data1.messung(1).datenreihe('U_A2').werte fre1_1, amp1_1 = analyse.fourier_fft(time1, vol1_1) fre1_2, amp1_2 = analyse.fourier_fft(time1, vol1_2) data2 = cassy.CassyDaten('daten/fundamental_2.lab') time2 = data2.messung(1).datenreihe('t').werte vol2_1 = data2.messung(1).datenreihe('U_A1').werte vol2_2 = data2.messung(1).datenreihe('U_A2').werte fre2_1, amp2_1 = analyse.fourier_fft(time2, vol2_1) fre2_2, amp2_2 = analyse.fourier_fft(time2, vol2_2) peak1_1 = analyse.peakfinder_schwerpunkt(fre1_1[5:600], amp1_1[5:600]) peak1_2 = analyse.peakfinder_schwerpunkt(fre1_2[5:600], amp1_2[5:600]) peak2_1 = analyse.peakfinder_schwerpunkt(fre2_1[5:600], amp2_1[5:600]) peak2_2 = analyse.peakfinder_schwerpunkt(fre2_2[5:600], amp2_2[5:600])
plt.ylabel(u"U1/V") plt.setp(ax1.get_xticklabels(), visible=False) plt.grid() ax2 = plt.subplot(gs[1, 0], sharex=ax1) plt.plot(t[j], U2[j], color='red') plt.ylabel(u"U2/V") plt.xlabel(u"t/s") ax2.yaxis.tick_right() plt.grid() plt.subplots_adjust(hspace=.0) plt.savefig("Images/Messung " + str(i) + "_" + name[j] + ".jpg") #Fourieranalyse w, A = analyse.fourier_fft(t[j], U1[j]) #w,A = analyse.fourier_fft(t[j],U2[j]) range1 = [(0.6, 0.63), (0.6, 0.63), (0.6, 0.63), (0.5, 0.64)] #in diesen beiden Bereichen sollen der erste range2 = [(0.6, 0.63), (0.63, 0.64), (0.64, 0.7), (0.69, 0.8)] #bzw. der zweite Peak "eingefangen" werden ind_w1 = list(A).index( max(A[np.argmax(w > range1[i - 1][0]):np.argmax( w > range1[i - 1][1])]), np.argmax(w > range1[i - 1][0])) ind_w2 = list(A).index( max(A[np.argmax(w > range2[i - 1][0]):np.argmax( w > range2[i - 1][1])]), np.argmax(w > range2[i - 1][0])) #Fourierplot plt.figure()
""" Created on Mon Sep 16 21:19:03 2019 @author: simon """ from praktikum import analyse from praktikum import cassy import numpy as np import matplotlib.pyplot as plt data = cassy.CassyDaten('daten/mittel_20_3.lab') timeVal = data.messung(1).datenreihe('t').werte pres = data.messung(1).datenreihe('p_A1').werte f, (ax1, ax2) = plt.subplots(2, 1) ax1.plot(timeVal, pres, color='red') freq, amp = analyse.fourier_fft(timeVal, pres) ax2.plot(freq[0:8000], amp[0:8000], color='red') ax1.set_ylabel("Druckdifferenz / hPa") ax2.set_ylabel("Amplitude") ax2.set_xlabel("$f$ / Hz") ax1.set_xlabel("$t$ / $s$") ax1.set_xlim(0, 8) ax2.set_xlim(0, 5) plt.rcParams["figure.figsize"] = (12, 6) plt.rcParams['axes.titlesize'] = 'large' plt.rcParams['axes.labelsize'] = 'large' f.tight_layout() plt.savefig('plots/rohdaten.pdf') plt.show() plt.close()
fplus = np.array([]) fminus = np.array([]) dic_file = {1:'ohne2', 2:'ohne3', 3:'eisen2'} dic_fre = {1: np.array([57,67,70,78]), 2:np.array([64,68,68,72]), 3:np.array([20,29,40,49])} freqs = np.array([]) amps = np.array([]) for i in range(1,4,1): data = cassy.CassyDaten('daten/schwebung_' + dic_file[i] + '_1.lab') time = data.messung(1).datenreihe('t').werte vol = data.messung(1).datenreihe('U_A1').werte vol2 = data.messung(1).datenreihe('U_A2').werte freq, amp = analyse.fourier_fft(time, vol) freq2, amp2 = analyse.fourier_fft(time, vol2) (freq, amp) = analyse.untermenge_daten(freq, amp, 0, 50000) (freq2, amp2) = analyse.untermenge_daten(freq2, amp2, 0, 50000) peak1 = analyse.peakfinder_schwerpunkt(freq[dic_fre[i][0]:dic_fre[i][1]], amp[dic_fre[i][0]:dic_fre[i][1]]) peak2 = analyse.peakfinder_schwerpunkt(freq[dic_fre[i][2]:dic_fre[i][3]], amp[dic_fre[i][2]:dic_fre[i][3]]) ymax1 = amp[dic_fre[i][0]:dic_fre[i][1]].argmax() ymax2 = amp[dic_fre[i][2]:dic_fre[i][3]].argmax() fplus = np.concatenate((fplus, np.array([peak1, np.abs(peak1-freq[dic_fre[i][0]+ymax1])]))) fminus = np.concatenate((fminus, np.array([peak2, np.abs(peak2-freq[dic_fre[i][2]+ymax2])]))) amps = np.concatenate((amps, amp)) freqs = np.concatenate((freqs, freq)) '''