def crosstalk_vad( speaker1_path, speaker2_path, frame_count, tha=30, thb=5, savgol_win=301, savgol_poly_order=1, ): """ tha: absolute dB level for when to consider there to be speech activity in a channel thb: minimum difference between channels to consider it to be one speaker only """ fs, x1 = wav.read(speaker1_path) _, x2 = wav.read(speaker2_path) x1 = x1.astype("float") x2 = x2.astype("float") # calculate rms energy in dB at a rate of 100 Hz (hop length 0.01 s) e1 = librosa.core.amplitude_to_db( librosa.feature.rms(x1, frame_length=int(fs * 0.02), hop_length=int(fs * 0.01))).flatten() e2 = librosa.core.amplitude_to_db( librosa.feature.rms(x2, frame_length=int(fs * 0.02), hop_length=int(fs * 0.01))).flatten() # boolean vectors at 100 Hz, s1: only speaker 1. s2: only speaker 2. s1 = np.logical_and(np.greater(e1, tha), np.greater(e1, e2 + thb)) s2 = np.logical_and(np.greater(e2, tha), np.greater(e2, e1 + thb)) smooth_s1 = savgol_filter( s1, savgol_win, savgol_poly_order, ) smooth_s2 = savgol_filter( s2, savgol_win, savgol_poly_order, ) s1x = np.clip(sig.resample(smooth_s1, frame_count, window="hamming"), 0, 1) s2x = np.clip(sig.resample(smooth_s2, frame_count, window="hamming"), 0, 1) s1x[s1x >= 0.1] = 1 s2x[s2x >= 0.1] = 1 s1x[s1x < 0.1] = 0 s2x[s2x < 0.1] = 0 return s1x, s2x
def plotVelocityAcc(time, x_ori, title): skip = 1 # x_ori velocity_ori, time_velocity_ori = computeVelocity( time, x_ori, n=skip, time_constant=True ) # velocity axis for every 55 ms (Ref: eye movement Frederick Bartlet) acceleration_ori = computeAcceleration( time_velocity_ori, velocity_ori, n=skip, time_constant=True) # compute acceleration # x_filtered # first derivative gaze_x_d1 = savgol_filter(x_ori[:, 0], AVG_WIN_SIZE, polyorder=3, deriv=1) gaze_y_d1 = savgol_filter(x_ori[:, 1], AVG_WIN_SIZE, polyorder=3, deriv=1) gaze_d1 = np.array([gaze_x_d1, gaze_y_d1]).transpose() #second derivative gaze_x_d2 = savgol_filter(x_ori[:, 0], AVG_WIN_SIZE, polyorder=3, deriv=2) gaze_y_d2 = savgol_filter(x_ori[:, 1], AVG_WIN_SIZE, polyorder=3, deriv=2) gaze_d2 = np.array([gaze_x_d2, gaze_y_d2]).transpose() velocity_filtered = np.sqrt(np.sum(np.power(gaze_d1, 2), -1)) acceleration_filtered = np.sqrt(np.sum(np.power(gaze_d2, 2), -1)) plt.figure(1) plt.plot(velocity_ori, label="Velocity original") plt.plot(velocity_filtered, label="Velocity filtered") plt.legend(loc="upper right") plt.title(title) plt.figure(2) plt.plot(acceleration_ori, label="Acceleration original") plt.plot(acceleration_filtered, label="Acceleration filtered") plt.legend(loc="upper right") plt.title(title) #power plt.figure(3) powerPlot(np.array(velocity_ori), np.array(velocity_filtered), 72, title) plt.figure(4) powerPlot(np.array(acceleration_ori), np.array(acceleration_filtered), 72, title)
def computeVelocityAccel(time, gaze, n, poly): # x_filtered # first derivative gaze_x_d1 = savgol_filter(gaze[:, 0], n, polyorder=poly, deriv=1) gaze_y_d1 = savgol_filter(gaze[:, 1], n, polyorder=poly, deriv=1) # time_d1 = savgol_filter(time, n, polyorder=poly, deriv=1) gaze_d1 = np.array([gaze_x_d1, gaze_y_d1]).transpose() #second derivative gaze_x_d2 = savgol_filter(gaze[:, 0], n, polyorder=poly, deriv=2) gaze_y_d2 = savgol_filter(gaze[:, 1], n, polyorder=poly, deriv=2) # time_d2 = savgol_filter(savgol_filter(time, n, polyorder=poly, deriv=0), n, polyorder=poly, deriv=1) gaze_d2 = np.array([gaze_x_d2, gaze_y_d2]).transpose() velocity_filtered = np.sqrt(np.sum(np.power(gaze_d1, 2), -1)) acceleration_filtered = np.sqrt(np.sum(np.power(gaze_d2, 2), -1)) return velocity_filtered, acceleration_filtered
def data_smooth(data,name,file_num): plt.figure(0) plt.plot(data,label='Before smoothing') data = savgol_filter(data, 31, 3) plt.plot(data, color='red',label='After smoothing') plt.xlabel('time series') plt.ylabel('attentions') plt.title('data smoothing') plt.legend(loc='upper right') plt.savefig(all_file + str(file_num+1) +"_" + name + ".png") plt.close(0) return data
def savgol_filter_v(v): filtered_v = savgol_filter(v, 33, 10) return filtered_v
def savgol_filter_ecg(ecg): filtered_ecg = savgol_filter(ecg, 33, 5) return filtered_ecg
def post(self): try: download_result: str = request.headers.get("download_result") req_json: dict = request.get_json() freq_start = req_json["frequency"]["start"] freq_end = req_json["frequency"]["end"] freq_num = req_json["frequency"]["num_data"] freqs = np.linspace(freq_start, freq_end, freq_num) d = req_json["absorber_thickness"] mr_real = req_json["relative_permeability"]["real"] mr_imag = req_json["relative_permeability"]["imag"] mr = np.complex(mr_real, mr_imag) er_real = req_json["relative_permitivity"]["real"] er_imag = req_json["relative_permitivity"]["imag"] er = np.complex(er_real, er_imag) show_impedance = req_json["option"]["show"]["impedance"] show_absorption = req_json["option"]["show"]["absorption"] savgol_length = req_json["option"]["savgol_filter"]["window_length"] savgol_polyorder = req_json["option"]["savgol_filter"]["polyorder"] Zreal_data = list() Zimag_data = list() RL_data = list() for freq in freqs: Z = impedance(freq, d, mr, er) Zreal_data.append(Z.real) Zimag_data.append(Z.imag) RL = absorptionV1(Z) RL_data.append(RL) response = dict() response["frequency"] = dict() response["frequency"]["label"] = ['{:.2e}'.format(i) for i in freqs] response["frequency"]["value"] = list(freqs) if show_impedance: response["impedance"] = dict() response["impedance"]["real"] = Zreal_data response["impedance"]["real_filter"] = list(savgol_filter(Zreal_data, savgol_length, savgol_polyorder)) response["impedance"]["imag"] = Zimag_data response["impedance"]["imag_filter"] = list(savgol_filter(Zimag_data, savgol_length, savgol_polyorder)) if show_absorption: response["reflection_loss"] = dict() response["reflection_loss"]["original"] = RL_data response["reflection_loss"]["filter"] = list(savgol_filter(RL_data, savgol_length, savgol_polyorder)) # create dataframe for calculation result and download it if download_result == "true": df = pd.DataFrame( list(zip( freqs, Zreal_data, Zimag_data, RL_data, )), columns=[ "Frequency (Hz)", "Impedance Real", "Impedance Imag", "Reflection Loss (dB)", ] ) resp = make_response(df.to_csv(index=None)) resp.headers["Access-Control-Expose-Headers"] = "Content-Disposition" resp.headers["Content-Disposition"] = "attachment; filename=simulation_result.csv" resp.headers["Content-Type"] = "text/csv" return resp return response, 200 except Exception as e: return CustomException(e)
def post(self): try: # reading formdata parameter download_result: str = request.headers.get("download_result") key = request.headers.get("key") body_json = request.get_json() thickness = np.float(body_json["thickness"]) lambda_0 = np.float(body_json["lambda_0"]) lambda_C = np.float(body_json["lambda_C"]) savgol_length = body_json["option"]["savgol_filter"]["window_length"] savgol_polyorder = body_json["option"]["savgol_filter"]["polyorder"] # get dataframe data = InputFile.load_from_redis(key) # initialize result lists resultFreqLabel = list() resultFreq = list() resultR_r = list() resultR_i = list() resultT_r = list() resultT_i = list() resultMr_r = list() resultMr_i = list() resultEr_r = list() resultEr_i = list() resultZ_r = list() resultZ_i = list() resultRL = list() # calculate loop for idx, freq in enumerate(data.frequency): s11 = data.s11[idx] s21 = data.s21[idx] resultFreq.append(freq) resultFreqLabel.append('{:.2e}'.format(freq)) R = reflectance(s11, s21) resultR_r.append(R.real) resultR_i.append(R.imag) T = transmitance(s11, s21) resultT_r.append(T.real) resultT_i.append(T.imag) delta = delta_const(thickness, T) # mr = relative_permeability(R, delta, lambda_0, lambda_C) mr = relative_permeability(R, delta, freq, lambda_C) resultMr_r.append(mr.real) resultMr_i.append(mr.imag) er = relative_permitivity(mr, T, thickness, lambda_0, lambda_C) resultEr_r.append(er.real) resultEr_i.append(er.imag) Z = impedance(freq, thickness, mr, er) resultZ_r.append(Z.real) resultZ_i.append(Z.imag) RL = absorption(Z, R) resultRL.append(RL) # create dataframe for calculation result and download it if download_result == "true": df = pd.DataFrame( list(zip( data.frequency, resultR_r, resultR_i, resultT_r, resultT_i, resultMr_r, resultMr_i, resultEr_r, resultEr_i, resultZ_r, resultZ_i, resultRL, )), columns=[ "Frequency (Hz)", "Reflectance Real", "Reflectance Imag", "Transmitance Real", "Transmitance Imag", "Permeability Real", "Permeability Imag", "Permitivity Real", "Permitivity Imag", "Impedance Real", "Impedance Imag", "Reflection Loss (dB)", ] ) resp = make_response(df.to_csv(index=None)) resp.headers["Access-Control-Expose-Headers"] = "Content-Disposition" resp.headers["Content-Disposition"] = "attachment; filename=experiment_result.csv" resp.headers["Content-Type"] = "text/csv" return resp return { "frequency": { "label": resultFreqLabel, "value": resultFreq, }, "reflectance": { "real": resultR_r, "real_filter": list(savgol_filter(resultR_r, savgol_length, savgol_polyorder)), "imag": resultR_i, "imag_filter": list(savgol_filter(resultR_i, savgol_length, savgol_polyorder)), }, "transmitance": { "real": resultT_r, "real_filter": list(savgol_filter(resultT_r, savgol_length, savgol_polyorder)), "imag": resultT_i, "imag_filter": list(savgol_filter(resultT_i, savgol_length, savgol_polyorder)), }, "relative_permeability": { "real": resultMr_r, "real_filter": list(savgol_filter(resultMr_r, savgol_length, savgol_polyorder)), "imag": resultMr_i, "imag_filter": list(savgol_filter(resultMr_i, savgol_length, savgol_polyorder)), }, "relative_permitivity": { "real": resultEr_r, "real_filter": list(savgol_filter(resultEr_r, savgol_length, savgol_polyorder)), "imag": resultEr_i, "imag_filter": list(savgol_filter(resultEr_i, savgol_length, savgol_polyorder)), }, "impedance": { "real": resultZ_r, "real_filter": list(savgol_filter(resultZ_r, savgol_length, savgol_polyorder)), "imag": resultZ_i, "imag_filter": list(savgol_filter(resultZ_i, savgol_length, savgol_polyorder)), }, "reflection_loss": { "original": resultRL, "filter": list(savgol_filter(resultRL, savgol_length, savgol_polyorder)), } } except Exception as e: return CustomException(e)
def post(self): try: download_result: str = request.headers.get("download_result") key = request.headers.get("key") req_json: dict = request.get_json() data = InputFile.load_from_redis(key, experiment_mode=False) d = req_json["absorber_thickness"] show_impedance = req_json["option"]["show"]["impedance"] show_absorption = req_json["option"]["show"]["absorption"] savgol_length = req_json["option"]["savgol_filter"]["window_length"] savgol_polyorder = req_json["option"]["savgol_filter"]["polyorder"] Zreal_data = list() Zimag_data = list() RL_data = list() for (idx, freq) in enumerate(data.frequency): mr = data.mr[idx] er = data.mr[idx] Z = impedance(freq, d, mr, er) Zreal_data.append(Z.real) Zimag_data.append(Z.imag) RL = absorptionV1(Z) RL_data.append(RL) response = dict() response["frequency"] = dict() response["frequency"]["label"] = ['{:.2e}'.format(i) for i in data.frequency] response["frequency"]["value"] = list(data.frequency) if show_impedance: response["impedance"] = dict() response["impedance"]["real"] = Zreal_data response["impedance"]["real_filter"] = list(savgol_filter(Zreal_data, savgol_length, savgol_polyorder)) response["impedance"]["imag"] = Zimag_data response["impedance"]["imag_filter"] = list(savgol_filter(Zimag_data, savgol_length, savgol_polyorder)) if show_absorption: response["reflection_loss"] = dict() response["reflection_loss"]["original"] = RL_data response["reflection_loss"]["filter"] = list(savgol_filter(RL_data, savgol_length, savgol_polyorder)) # create dataframe for calculation result and download it if download_result == "true": df = pd.DataFrame( list(zip( data.frequency, Zreal_data, Zimag_data, RL_data, )), columns=[ "Frequency (Hz)", "Impedance Real", "Impedance Imag", "Reflection Loss (dB)", ] ) resp = make_response(df.to_csv(index=None)) resp.headers["Access-Control-Expose-Headers"] = "Content-Disposition" resp.headers["Content-Disposition"] = "attachment; filename=simulation_with_input_parameter_result.csv" resp.headers["Content-Type"] = "text/csv" return resp return response, 200 except Exception as e: return CustomException(e)
dictlist = [] for key, value in pcount.items(): temp = [key, value] dictlist.append(temp) col = [row[1] for row in dictlist] ticks = [] xs = np.arange(0, 1.1, 0.1) for x in xs: ticks.append(str(round(x, 2))) plt.xticks(np.arange(0, 11), ticks) yhat = _savitzky_golay.savgol_filter(test, 9, 3) # window size 73, polynomial order 2 plt.subplot(211) plt.xlabel(r'$p$') plt.ylabel(r'$P_{G}$') plt.xticks(np.arange(0, 11), ticks) plt.grid(True) plt.plot(yhat, "r-") plt.title("A", loc='left') pos = np.where(np.asarray(test2) <= 0.5) pos = np.array(pos).ravel().tolist() for i in pos: if (i > 0 and i < 8):
def update_figure(self): # reset figure self.figure.clear() # check if different y labels exist labels = [''.join(readings.unit) for _, readings in self.data.items()] uniform = False if len(labels) > 0: uniform = labels.count(labels[0]) == len(labels) # create an axis ax = self.figure.add_subplot(111) ax.set_xlabel(r'Wellenlänge $\lambda$ in nm') if uniform: if self.standard: ax.set_ylabel('Normierte ' + list(self.data.values())[0].unit[0] + ' in %') else: ax.set_ylabel(labels[0]) else: if self.standard: ax.set_ylabel( 'Achtung: Unterschiedliche Einheiten. (normiert auf 100 %)') elif len(labels) != 0: QMessageBox.warning(None, 'Die Einheiten der eingelesenen Dateien unterscheiden sich', '''Möglicherweise ist eine Vergleichbarkeit der Daten nicht gewährleistet. ''') ax.set_ylabel('Achtung: Unterschiedliche Einheiten.') # create grid ax.grid() # plot data for file_name, readings in self.data.items(): yvalues = copy.deepcopy(readings.y) if self.standard: if readings.ymax != 0: yvalues = (yvalues - readings.ymin) / \ (readings.ymax - readings.ymin) * 100 # yvalues = yvalues / readings.ymax * 100 ax.plot(readings.wavelength, yvalues, label=os.path.basename(readings.path).replace('.txt', '')) # peaks if self.show_peaks: smooth_data = sg.savgol_filter( yvalues, window_length=31, polyorder=3) maxima = argrelmax(smooth_data) for i in np.nditer(maxima): ax.scatter([readings.wavelength[i]], [ smooth_data[i]], 10, color='black') ax.plot([readings.wavelength[i], readings.wavelength[i]], [readings.ymin, smooth_data[i]], color='black') if self.standard: peak_label = str(readings.wavelength[i]) + ' nm' else: peak_label = '(' + str(readings.wavelength[i]) + '|' + str( round(smooth_data[i], 2)) + ')' ax.annotate(peak_label, xy=(readings.wavelength[i], yvalues[i]), xytext=(+0, +10), textcoords='offset pixels') # legend ax.legend() # refresh canvas self.canvas.draw()
import scipy.signal._savitzky_golay as sg import numpy as np import matplotlib.pyplot as plt import pandas as pd data = pd.read_csv( r'C:\Users\TeerM\OneDrive\Desktop\Research\Codes\PycharmProjects\untitled\Oriented data.csv', index_col=0) print(data.head()) for j in range(len(data.columns)): f, (ax1, ax2) = plt.subplots(1, 2, sharey='row') ax2.plot(sg.savgol_filter(data.iloc[20500:21000, j], 99, 7)) ax2.set_title(data.columns[j]) ax1.plot(data.iloc[20500:21000, j]) ax1.set_title(data.columns[j]) plt.show()
down_sample = data.loc[data.index % 2 == 1] down_avg_data = pd.DataFrame(columns=down_sample.columns.values) down_sample = down_sample[(down_sample.GazeX >= 0) & (down_sample.GazeY >= 0)] for col in down_sample: #applying savgol filter #window= 3 samples (3/72 = 41.6 ms) with poly order = 2 #ref:https://doi.org/10.3758/BRM.42.1.188 if col in normalize_col: down_avg_data[col] = down_sample[col].values else: down_avg_data[col] = savgol_filter(down_sample[col].values, AVG_WIN_SIZE, polyorder=3) # powerPlot(down_sample["GazeX"].values[1000:1200], down_avg_data["GazeX"].values[1000:1200], 72, "n=3, poly=2") #power plot # gaze_avg = np.array([down_sample["GazeX"].values[1000:1200], down_sample["GazeY"].values[1000:1200]]).transpose() # gaze_avg_f = np.array( # [down_avg_data["GazeX"].values[1000:1200], down_avg_data["GazeY"].values[1000:1200]]).transpose() # plotVelocityAcc(down_sample["Time"].values[1000:1200], gaze_avg, "n=3, poly=2") #velocity and acc plot #remove object < 0 removed_idx = (down_avg_data["ObjectX"].values < 0.1) | (down_avg_data["ObjectY"].values < 0.1) down_avg_data["ObjectX"].loc[removed_idx] = -1 down_avg_data["ObjectY"].loc[removed_idx] = -1 #compute velocity and acceleration using Savgol