def Audio_mute(A_cut,B_cut=[]): if config.channel==1: # plt.plot(config.A_cut) # plt.title('mute ramp sample to analyze') # plt.show() mute_counter=0 # print(len(config.A_cut)) for i in A_cut[config.muteliveposition::]: if i!=0: ret=control.mag2db(abs(i)) else: config.counter += 1 continue if config.live_signal==1 and config.muteliveposition >= len(A_cut)-1: # print(config.muteliveposition) config.muteliveposition=0 return config.linkpositions if ret < control.mag2db(config.mute_treshold): config.mutestate=True config.counter += 1 config.reset=0 config.muteliveposition + 1 continue elif ret >= control.mag2db(config.mute_treshold) and mute_counter <= 4100: config.mutestate = False mute_counter+=1 continue elif ret >= control.mag2db(config.mute_treshold) and config.reset == 0 and config.counter > 4100: # mute dluzszy niz 300 ms (4100 to 100 ms) config.mutestate = False config.mute_detect_samplingAudio = 1 if config.live_signal == 1: config.linkpositions.insert(0, f'{int((config.counter / config.fs) * 1000)}{"ms"}') if len(config.linkpositions)>3: config.linkpositions.pop() else: config.mutestate=False config.linkpositions.append("{} ms".format(int((config.counter / config.fs) * 1000))) mute_counter=0 config.counter = 0 config.reset=1 config.muteliveposition + 1 # if config.channel != 1: # Audio_mute() continue else: config.mutestate = False mute_counter = 0 config.counter=0 config.muteliveposition + 1 continue return config.linkpositions else: for i in [*A_cut[-20000:], *B_cut[:20000]]: if abs(i) < control.mag2db(config.mute_treshold): config.counter += 1 if config.counter > 5000:#It is around 100 ms of mute state. return "-1" else: config.counter = 0 return "0"
def Audio_level(param, A_cut, B_cut=[]): if config.channel == 1: try: ret = np.amax(A_cut) if ret == 0: return (f'{0}{"DB"}') decibelAmax = int( 20 * np.log10(np.sqrt(np.mean( np.absolute(ret)**2)))) #for maksimum value <- this decibelAaverage = int(control.mag2db(np.average( np.abs(A_cut)))) # for average value <- this if decibelAaverage <= int(control.mag2db(config.mute_treshold)): return "Mute" else: return "{} DB max {} DB ".format(decibelAaverage, decibelAmax) except OverflowError: ret = (f'{0}{"DB"}') if config.server_adress != 0: config.server_adress.send( bytes(str(ret) + '\r\n', encoding='utf-8')) else: if param == 1: A1 = A_cut[-88200:-10000] B1 = B_cut[10000:88200] else: A1 = A_cut[-88200:] B1 = B_cut[:88200] decibelAmax = int( 20 * np.log10(np.sqrt(np.mean(np.absolute(np.max(A1))**2)))) decibelAaverage = int(control.mag2db(np.average(np.abs(A1)))) decibelBmax = int( 20 * np.log10(np.sqrt(np.mean(np.absolute(np.max(B1))**2)))) decibelBaverage = int(control.mag2db(np.average(np.abs(B1)))) diff = np.absolute(decibelAaverage) - np.absolute(decibelBaverage) if diff >= 5: return "{} ,first average: {}DB max: {}DB second average: {}DB max: {}DB".format( -1, decibelAaverage, decibelAmax, decibelBaverage, decibelBmax) else: return "{} ,first average: {}DB max: {}DB second average: {}DB max: {}DB".format( 0, decibelAaverage, decibelAmax, decibelBaverage, decibelBmax)
def question2(OLTF, plot=True): print(spacer + "Question 2" + spacer) # 1. BODE PLOT plt.figure("Q2 - Bode plot") ct.bode_plot(OLTF, dB=True, margins=True) if plot: plt.show() # 2. GAIN gainHz = ct.stability_margins(OLTF) gaindB = ct.mag2db(gainHz) print("\t The maximum gain in Hz is {} and in dB is {}".format( gainHz, gaindB)) return (gainHz, gaindB)
def sine_detect(sample,chunk): counter=0 boundary_value=(1200*chunk)/22050 if config.live_signal != 1: for i in sample: if abs(i) < control.mag2db(config.mute_treshold): counter += 1 if counter > 5000: # It is around 100 ms of mute state. config.mute_detect_samplingAudio=1 return -2 fft = np.abs(np.fft.fft(sample)) average = np.average(fft) counter = 0 for i, j in enumerate(fft): # i pozycja aktualne, j to wartosc if i > len(fft) / 2: break if j > average: counter = counter + 1 continue if counter > boundary_value: return -1 return 0
header_row.append("chunk"+str(i+1)) print("header_row: ",header_row) with open('Results.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerow(header_row) for i, chunk in enumerate(audio_chunks): out_file = ".//splitAudio//chunk{0}.wav".format(i) print ("exporting", out_file) chunk.export(out_file, format="wav") ##Peak Amplitude of each audio chunk chunks_peak_amp_row.append(chunk.max) chunks_peak_amp_row_db.append(control.mag2db(chunk.max)) chunks_peak_amp_row_dBFS.append(chunk.max_dBFS) ##Time duration of each audio chunk chunks_time_duration.append(str(chunk.duration_seconds)+" sec") ##Frame count of each audio chunk chunks_frame_count.append(chunk.frame_count(ms=None)) ##Total sum of audio lengths of all audio chunks total_audio_time = total_audio_time + chunk.duration_seconds writer.writerow(chunks_peak_amp_row) writer.writerow(chunks_peak_amp_row_db) writer.writerow(chunks_peak_amp_row_dBFS) writer.writerow(chunks_time_duration)
# flag to define if using dB or absolute scale for M(omega) dB_flag = False # assigning plant and controller from past HW (to make sure we don't introduce additional errors) Plant = P16.Plant C_pid = P16.C_pid if __name__ == "__main__": # display bode plots of transfer functions fig1 = plt.figure() bode([Plant, Plant*C_pid, Plant*C_pid/(1+Plant*C_pid)], dB=dB_flag) plt.legend(('No control - $P(s)$', '$C_{pid}(s)P(s)$', 'Closed-loop PID')) fig1.axes[0].set_title('Mass Spring Damper') fig2 = plt.figure() bode([Plant, Plant*C_pid], dB=dB_flag, margins=True) fig2.axes[0].set_title('Mass Spring Damper - Stability Margins') # Calculate the phase and gain margin gm, pm, Wcg, Wcp = margin(Plant * C_pid) if dB_flag: print("gm: ", gm, " pm: ", pm, " Wcg: ", Wcg, " Wcp: ", Wcp) else: print("gm: ", mag2db(gm), " pm: ", pm, " Wcg: ", Wcg, " Wcp: ", Wcp) plt.show()