def get_best_I(f, sampling_rate, act_result, n=5): if n > len(act_result): return act_result best_I = np.zeros((n, 4)) scores = np.zeros(n) for I in act_result: tc, fc, c, dt_ = I chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt_, length=len(f), sampling_rate=sampling_rate) score = chirplet.chirplet_transform(f) i = 0 while i < n and score < scores[i]: i += 1 if i < n: new_best_I = np.copy(best_I) new_scores = np.copy(scores) new_best_I[i] = I new_scores[i] = score for j in range(i + 1, n): new_best_I[j] = best_I[j - 1] new_scores[j] = scores[j - 1] best_I = np.copy(new_best_I) scores = np.copy(new_scores) return best_I
def swiping_fixed_frequency(signal, sampling_rate=1.0, dt=1.0, frequency_number=200, number_of_estimations=200): # frequency_number: number of frequencies to try on the signal N = len(signal) fc_range = ( np.arange(frequency_number + 1) / frequency_number ) * sampling_rate / 2 # Frequency ranges from 0 to sampling_rate print("Range of frequencies:", fc_range[0], "to", fc_range[-1]) fc_test = np.zeros((frequency_number + 1, number_of_estimations)) fc_true = np.zeros(number_of_estimations) for index in range(number_of_estimations): i0 = int(index * (N - dt * sampling_rate) / number_of_estimations) windowed_signal = signal[i0:i0 + int(dt * sampling_rate)] act = ACT(windowed_signal, sampling_rate=sampling_rate) for i in range(frequency_number + 1): fc = fc_range[i] c = act.best_fitting_chirpiness(dt / 2, fc, dt) chirplet = Chirplet(tc=dt / 2, fc=fc, c=c, dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False) fc_test[i][index] = chirplet.chirplet_transform(windowed_signal) fc_true[index] = frequency[int(i0 + sampling_rate * dt / 2)] fig, (ax1, ax2) = plt.subplots(nrows=2) ax1.plot(dt / 2 + np.arange(number_of_estimations) * (N - dt * sampling_rate / 2) / (sampling_rate * number_of_estimations), fc_true, label="True frequency") ax1.set_xlabel('Time [sec]') ax1.set_ylabel('Frequency [Hz]') ax1.legend() ax2.imshow(np.flip(fc_test, axis=0), extent=(dt / 2, N / sampling_rate - dt / 2, fc_range[0], fc_range[-1]), aspect=(N - dt * sampling_rate) * dt / (2 * sampling_rate**2)) ax2.set_xlabel('Time [sec]') ax2.set_ylabel('Frequency [Hz]') plt.show()
def get_best_chirplet(f, I_list, length=500, sampling_rate=500): best_I = I_list[0] best_sc = 0 for I in I_list: tc, fc, c, dt = I chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt, length=length, sampling_rate=sampling_rate) sc = chirplet.chirplet_transform(f) if sc > best_sc: best_I = I best_sc = sc return best_I
def swiping_fixed_chirpiness(signal, sampling_rate=1.0, dt=1.0, chirpiness_number=200, number_of_estimations=200): # chirpiness number: number of chirpiness (excluding 0) to try on the signal N = len(signal) c_range = ( np.arange(chirpiness_number + 1) / chirpiness_number - 0.5 ) * sampling_rate / dt # Chripiness ranges from -sampling_rate/(dt) to sampling_rate/(dt) print("Chirpiness range:", c_range[0], "to", c_range[-1]) """ fig, (ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8) = plt.subplots(nrows=8) ax1.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(0*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax2.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(1*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax3.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(2*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax4.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(3*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax5.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(4*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax6.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(5*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax7.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(6*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) ax8.plot(np.arange(int(sampling_rate*dt))/sampling_rate, Chirplet(tc=dt / 2, fc=sampling_rate/8, c=c_range[int(7*number_of_estimations/8)], dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False).chirplet) plt.show() """ c_test = np.zeros((chirpiness_number + 1, number_of_estimations)) c_true = np.zeros(number_of_estimations) for index in range(number_of_estimations): i0 = int(index * (N - dt * sampling_rate) / number_of_estimations) windowed_signal = signal[i0:i0 + int(dt * sampling_rate)] act = ACT(windowed_signal, sampling_rate=sampling_rate) for i in range(chirpiness_number + 1): c = c_range[i] fc = act.best_fitting_frequency(dt / 2, c) chirplet = Chirplet(tc=dt / 2, fc=fc, c=c, dt=dt, length=int(sampling_rate * dt), sampling_rate=sampling_rate, gaussian=False) c_test[i][index] = chirplet.chirplet_transform(windowed_signal) c_true[index] = (frequency[int(i0 + sampling_rate * dt)] - frequency[i0]) / dt c_test = c_test + np.flip(c_test, axis=0) fig, (ax1, ax2) = plt.subplots(nrows=2) ax1.plot(dt / 2 + np.arange(number_of_estimations) * (N - dt * sampling_rate / 2) / (sampling_rate * number_of_estimations), c_true, label="True chirpiness") ax1.set_xlabel('Time [sec]') ax1.set_ylabel('Chirpiness [Hz^2]') ax1.legend() ax2.imshow(np.flip(c_test, axis=0), extent=(dt / 2, N / sampling_rate - dt / 2, c_range[0], c_range[-1]), aspect=(N - dt * sampling_rate) * dt / (4 * sampling_rate**2)) ax2.set_xlabel('Time [sec]') ax2.set_ylabel('Chirpiness [Hz^2]') plt.show()
def chirplet_transform(f, t0, starting_frequency, ending_frequency, dt): c = (ending_frequency - starting_frequency)/dt chirplet = Chirplet(tc=t0+dt/2, fc=starting_frequency+c*dt/2, c=c, dt=dt, length=len(f), sampling_rate=self.sampling_rate, gaussian=False) return chirplet.chirplet_transform(f)
def get_Imax(Rnf, tc_start, fc_start, c_start, dt, alpha=5e-1, beta=0.95, max_iter=5000, fixed_tc=True): alpha_tc = 0.2*alpha*dt*self.sampling_rate/len(Rnf) sc = Chirplet(tc=tc_start, fc=fc_start, c=c_start, dt=dt, length=self.length, sampling_rate=self.sampling_rate, gaussian=False)\ .chirplet_transform(Rnf) delta_sc = sc i = 0 while delta_sc > 1e-3 and i < 100: fc_start = self.best_fitting_frequency(tc_start, c_start, signal=Rnf, fc_range=fc_range) c_start = self.best_fitting_chirpiness(tc_start, fc_start, dt, signal=Rnf, c_range=c_range) new_sc = Chirplet(tc=tc_start, fc=fc_start, c=c_start, dt=dt, length=self.length, sampling_rate=self.sampling_rate, gaussian=False)\ .chirplet_transform(Rnf) delta_sc = np.abs(sc - new_sc) sc = new_sc i += 1 chirplet = Chirplet(tc=tc_start, fc=fc_start, c=c_start, dt=dt, length=self.length, sampling_rate=self.sampling_rate) dtc, dfc, dc = chirplet.derive_transform_wrt_tc(Rnf), chirplet.derive_transform_wrt_fc( Rnf), chirplet.derive_transform_wrt_c(Rnf) dtc, dfc, dc = np.real(dtc), np.real(dfc), np.real(dc) vtc, vfc, vc = alpha_tc * dtc, alpha * dfc, alpha * dc if tc_start + vtc < 0 or tc_start + vtc > dt: vtc = 0 if fc_start + vfc < 0 or fc_start + vfc > 0.5 * self.sampling_rate: # Maximum frequency: sampling_rate/2 vfc = 0 if c_start + vc < 0 or c_start + vc > 0.5 * self.sampling_rate / dt: # Maximum chirpiness: sampling_rate/(2*dt) vc = 0 fc, c = fc_start + vfc, c_start + vc if not fixed_tc: tc = tc_start + vtc else: tc = tc_start nb_iter = 0 fc_list = [fc_start, fc] c_list = [c_start, c] tc_list = [tc_start, tc] sc_list = [chirplet.chirplet_transform(Rnf)] while (np.abs(vfc) >= 1e-4 or np.abs(vc) >= 1e-4) and nb_iter < max_iter: chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt, length=self.length, sampling_rate=self.sampling_rate) dtc, dfc, dc = chirplet.derive_transform_wrt_tc(Rnf), chirplet.derive_transform_wrt_fc(Rnf), chirplet.derive_transform_wrt_c(Rnf) dtc, dfc, dc = np.real(dtc), np.real(dfc), np.real(dc) vtc, vfc, vc = beta*vtc + alpha_tc*dtc, beta*vfc + alpha*dfc, beta*vc + alpha*dc if tc + vtc < 0 or tc + vtc > dt: vtc = 0 if fc + vfc < 0 or fc + vfc > 0.5*self.sampling_rate: # Maximum frequency: sampling_rate/2 vfc = 0 if c + vc < 0 or c + vc > 0.5*self.sampling_rate/dt: # Maximum chirpiness: sampling_rate/(2*dt) vc = 0 fc, c = fc + vfc, c + vc if not fixed_tc: tc = tc + vtc tc_list.append(tc) fc_list.append(fc) c_list.append(c) sc_list.append(chirplet.chirplet_transform(Rnf)) nb_iter += 1 chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt, length=self.length, sampling_rate=self.sampling_rate) sc = chirplet.chirplet_transform(Rnf) sc_list.append(sc) #plt.plot(np.arange(len(tc_list)), [self.sampling_rate*x for x in tc_list], label="Center time") #plt.plot(np.arange(len(fc_list)), fc_list, label="Center frequency") #plt.plot(np.arange(len(c_list)), c_list, label="Chirpiness") #plt.plot(np.arange(len(sc_list)), [self.sampling_rate*x/4 for x in sc_list], label="Chirplet transform") #plt.legend() #plt.show() if sc != sc: tc, fc, c = 0, 0, 0 return tc, fc, c, dt
act = ACT(signal, sampling_rate=sampling_rate, P=100) act.plot_wigner_distribution(title="Target signal") act_mp = act.act_gradient_descent(tc=T / 2, dt=dt / 5, fixed_tc=False) approximation_mp = np.zeros(N) for I in get_best_I(signal, sampling_rate, act_mp, n=10): tc, fc, c, dt_ = I chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt_, length=N, sampling_rate=sampling_rate) print(I, chirplet.chirplet_transform(signal)) approximation_mp += chirplet.chirplet_transform(signal) * np.real( chirplet.chirplet) plt.plot(np.arange(N) / sampling_rate, approximation_mp, label="MP approximation") plt.title("MP approximation") plt.legend() plt.show() ACT(approximation_mp, sampling_rate=sampling_rate).plot_wigner_distribution(title="MP algorithm") approximation_mp_lem = np.zeros(N)
alg = 4 # Greedy act if alg == 0: act_res = act.act_greedy(tc=0.3, dt=dt) elif alg == 1: act_res = act.act_exhaustive(tc=0.3, dt=dt) elif alg == 2: act_res = act.act_gradient_descent(tc=0.3, dt=dt, fixed_tc=True) elif alg == 3: act_res = act.act_gradient_descent(tc=0.5, dt=dt, fixed_tc=False) else: act_res = act.act_steve_mann(t0=0, dt=dt) approximation = np.zeros(N) for I in act_res: tc, fc, c, dt = I chirplet = Chirplet(tc=tc, fc=fc, c=c, dt=dt, length=N, sampling_rate=sampling_rate) print(I, chirplet.chirplet_transform(chirp.chirplet)) approximation += chirplet.chirplet_transform(chirp.chirplet) * np.real( chirplet.chirplet) ACT(approximation, sampling_rate=sampling_rate).plot_wigner_distribution( title="Gradient descent")