def test_definition(self): for n in [16,17,64,127]: x = arange(n)*2*pi/n y = ihilbert(sin(x)) y1 = direct_ihilbert(sin(x)) assert_array_almost_equal(y,y1) assert_array_almost_equal(ihilbert(sin(2*x)), direct_ihilbert(sin(2*x)))
def test_random_odd(self): for n in [33,65,55]: f = random((n,)) af = sum(f,axis=0)/n f = f-af assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(ihilbert(hilbert(f)),f) assert_array_almost_equal(hilbert(ihilbert(f)),f)
def calc_edge(numpeaks, ene, pdos): import numpy as np from scipy import fftpack # # take upper edge by inverse Hilbert transform # ih = fftpack.ihilbert(pdos) ihpeaks = findpeak(ene, abs(ih)) upper_edge = np.zeros(numpeaks) lower_edge = np.zeros(numpeaks) for peak in ihpeaks: if ih[peak] > 0.0 and ih[peak] > 0.8 * max( ih): # just choose large peak, positive part upper_edge = np.insert(upper_edge, 0, ene[peak]) elif ih[peak] <= 0.0 and ih[peak] < 0.8 * min(ih): lower_edge = np.insert(lower_edge, 0, ene[peak]) upper_edge = upper_edge[0:numpeaks] lower_edge = lower_edge[0:numpeaks] #upper_edge = upper_edge[::-1] #lower_edge = lower_edge[::-1] return upper_edge, lower_edge
def test_itilbert_relation(self): for n in [16,17,64,127]: x = arange(n)*2*pi/n f = sin(x)+cos(2*x)*sin(x) y = ihilbert(f) y1 = direct_ihilbert(f) assert_array_almost_equal(y,y1) y2 = itilbert(f,h=10) assert_array_almost_equal(y,y2)
def test_random_even(self): for n in [32,64,56]: f = random((n,)) af = sum(f,axis=0)/n f = f-af # zeroing Nyquist mode: f = diff(diff(f,1),-1) assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f) assert_array_almost_equal(hilbert(ihilbert(f)),f)
def compute_group_vel(ccf_full, ccf, t_ccf, dist, tmin, tmax, vmin, vmax, alpha): #构建中心周期序列 #per=np.arange(tmin,tmax+0.1,0.1) delta = 0.1 per = [] cper = tmin while cper < tmax: per.append(cper) cper += delta delta *= 1.01 if per[-1] < tmax: per.append(tmax) #将 ccf 变换到频率域 ccf_freq = fftpack.ifft(ccf) freq_samp = 2 * np.pi * abs(fftpack.fftfreq(len(ccf))) ccf_full_freq = fftpack.ifft(ccf_full) freq_full_samp = 2 * np.pi * abs(fftpack.fftfreq(len(ccf_full))) #时频分析 SSNR = [] t_ariv = [] ph_ariv = [] per_inst = [] omg_inst = [] group_vel = [] ftan_mat = [] for tn in per: omgn = 2 * np.pi / tn #窄带滤波 ccf_freq_nbG=ccf_freq*np.exp(-alpha*((freq_samp-omgn)\ /omgn)**2) ccf_full_freq_nbG=ccf_full_freq*\ np.exp(-alpha*((freq_full_samp-omgn)/omgn)**2) #变换到时间域 ccf_time_nbG = fftpack.fft(ccf_freq_nbG).real ccf_full_time_nbG = fftpack.fft(ccf_full_freq_nbG).real ccf_time_nbG_hilbert = fftpack.ihilbert(ccf_time_nbG) #计算包络 env = np.sqrt(ccf_time_nbG**2 + ccf_time_nbG_hilbert**2) #amp=20*np.log10(env) #单位:DB amp = env.copy() [env, t_env] = bspline_itp(env, t_ccf) #计算相位 phase = np.arctan(ccf_time_nbG_hilbert / ccf_time_nbG) for i in range(len(phase) - 1): k = int(abs(phase[i + 1] - phase[i]) / np.pi + 1 / 2) phase[i + 1] = phase[i + 1] + np.pi * k [phase, t_phase] = linear_itp(phase, t_ccf) #计算瞬时频率 index = np.where(env == np.max(env))[0][0] try: omgn = (phase[index + 1] - phase[index]) / (t_phase[index + 1] - t_phase[index]) except: continue if omgn < 0: continue omg_inst.append(omgn) t_ariv.append(t_env[index]) while phase[index] > np.pi / 2: phase[index] = phase[index] - np.pi ph_ariv.append(phase[index]) #计算群速度 per_inst.append(float(str(round(2 * np.pi / omg_inst[-1], 3)))) group_vel.append(dist / t_ariv[-1]) ftan_mat.append(list(amp)) #计算谱信噪比 index1 = int(dist / vmax) index2 = int(dist / vmin) signal = ccf_full_time_nbG[index1:index2 + 1] noise = ccf_full_time_nbG[index1 + 1000:index2 + 1001] noise_rms = np.sqrt(np.sum(noise**2) / len(noise)) SSNR.append(np.max(signal) / noise_rms) #print(tn,2*np.pi/omgn) #plt.subplot(211) #plt.plot(t_ccf,ccf,'k') #plt.subplot(212) #plt.plot(amp,'k') #plt.show() ftan_mat = np.transpose(ftan_mat) ftan_mat = 100 * ftan_mat / np.max(ftan_mat) #调整振幅(开方乘十),为了画图好看 tmp = [] for line in ftan_mat: tmp.append(np.sqrt(np.sqrt(line) * 10) * 10) ftan_mat = tmp.copy() #plt.plot(per_inst,SSNR) #plt.show() return (ftan_mat, group_vel, per_inst, omg_inst, t_ariv, ph_ariv, SSNR)
height_vir.append(peak[1]) width_vir.append(peak[2]) area_vir.append(peak[1] * peak[2] * np.sqrt(np.pi)) # # if you want to check by eye # if check: import matplotlib.pylab as plt import seaborn as sb from scipy import fftpack #print("upper edge:{}".format(upper_edge)) #print("lower edge:{}".format(lower_edge)) fit = fit_func(ene, *params) ih = fftpack.ihilbert(pdos) sb.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=False, rc=None) plt.plot(ene, fit, label="fitted") plt.plot(ene, pdos, label="original") plt.vlines(position_occ, 0, np.max(pdos), linestyle="dashed", linewidth=0.5)