使用fft对相位进行unwrap

如果复数相位是另一个量,例如频率,的线性函数,而我们想对相位进行直线拟合,那么我们首先需要对相位进行unwrap,然后才能进行拟合。numpy.unwrap可以对角度进行unwrap,但是如果有大段的数据缺失,numpy.unwrap会失效,我利用fft实现了一种新的unwrap的方法,如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaksdef fft_unwrap(freq, phs, pk_th=0.0, unwrap_th=1.5*np.pi, precision=1.e-3):'''Unwrap phs assuming phs is linear function wrt freq. freq: array_like, phs = a*freq + bphs: array_like, in rad, phase to unwrapunwrap_th: float, if the phase range is smaller than 2*pi, fft will break down, and we use numpy.unwrap insteadprecision: float, we will padding 0 to make the k after fft have interval smaller than precision.return: ndarray, phs after unwrap'''# phs in radisort = np.argsort(freq)freq = np.array(freq)[isort]phs = np.array(phs)[isort]dfreq = freq[1]-freq[0]y = np.exp(1.J*phs)n = 1./(precision*dfreq)n = int(2**np.ceil(np.log2(n)))if n < y.shape[0]:n = y.shape[0]valid = np.isfinite(y)y0 = y[valid][0]phs0 = phs[valid][0]freq0 = freq[valid][0]y = y/y0 # normalizey[~valid] = 0.y = y - y.mean()fft = np.fft.fft(y, n=n)fftfreq = np.fft.fftfreq(fft.shape[0], d=dfreq)fft = np.abs( np.fft.fftshift(fft) )fftfreq = np.fft.fftshift(fftfreq)*2*np.pi#plt.figure()#plt.plot(fftfreq, fft)ipk, pk_dict = find_peaks(fft, height=0.)pk_heights = pk_dict['peak_heights']isort = np.argsort(pk_heights)pk_heights = pk_heights[isort]ipk = ipk[isort]#print(fftfreq[ipk])#print('highest peak: %s'%pk_heights[-1])unwrap_phs = np.unwrap(phs[valid])print('second highest peak: %s (%.2f of highest)'%(pk_heights[-2], pk_heights[-2]/pk_heights[-1]) )print('maximum phase difference after np.unwrap: %s'%(unwrap_phs.max()-unwrap_phs.min()))if pk_heights[-2]>pk_heights[-1]*pk_th and unwrap_phs.max()-unwrap_phs.min()<unwrap_th:# all in one period#plt.figure()#plt.plot(freq, phs, 'o', label='data')phs[valid] = np.unwrap(phs[valid])#plt.plot(freq, phs, '.', label='unwrap')#plt.legend()#plt.show()return phselse:k = fftfreq[ipk[-1]]phs_pred = phs0 + k*(freq-freq0)dphs = phs_pred - phsnphs = np.around(dphs/2./np.pi)#plt.figure()#plt.plot(freq, phs, 'o', label='data')phs = phs + nphs*2*np.pi#plt.plot(freq, phs, '.', label='unwrap')#plt.plot(freq, phs_pred, '-', label='pred')#plt.legend()#plt.show()return phsif __name__ == '__main__':for ii in range(10):x = np.linspace(0., 1.8*np.pi, 201) + (np.random.rand()-0.5)*2*np.piw = 1.ns = ( np.random.rand(*x.shape) - 0.5 )*1.5phs0 = w*x#mask = int(x.shape[0]/1.5)#inds = np.random.choice(np.arange(x.shape[0]), replace=False, size=mask)inds1 = np.arange(30, 50)inds2 = np.arange(60, 80)inds3 = np.arange(100,120)inds = np.concatenate([inds1, inds2, inds3])phs0[inds] = np.nany = np.exp(1.J*(phs0 + ns))phs = np.angle(y)phs1 = fft_unwrap(x, phs)plt.plot(x, phs1, 'o', label='unwrap')plt.plot(x, phs, '*', label='phs')plt.plot(x, w*x, '.', label='phs0')plt.legend()plt.show()

使用fft对相位进行unwrap相关推荐

  1. (转载)关于FFT的相位谱

    转自于http://blog.sina.com.cn/s/blog_4a854d240100vdmq.html 先看一下我收到的程序,作为研究对象的信号是这样产生的: T=128; N=128; dt ...

  2. 基于强度传输方程的相位解包裹算法

    一.引言 2016年印度理工学院的 pandey等首次提出了基于强度传播方程的相位解包裹算法.该算法通过将包裹相位生成一个复数场,之后将其传播到一段距离.从而在复数场中紧密间隔的平面上模拟两个强度图像 ...

  3. Matlab参考函数

    附录1 常用命令 附录1.1 管理用命令 函数名 功能描述 函数名 功能描述 addpath 增加一条搜索路径 rmpath 删除一条搜索路径 demo 运行Matlab演示程序 type 列出.M文 ...

  4. matlab库函数大全

    附录 MATLAB函数参考 附录1 常用命令 附录1.1 管理用命令 函数名 功能描述 函数名 功能描述 addpath 增加一条搜索路径 rmpath 删除一条搜索路径 demo 运行Matlab演 ...

  5. MATLAB函数大全 .

    http://pleasaunce.blog.sohu.com/94162301.html   网上看到的,很珍贵,怕人家删了,就自己考过来了 附录1 常用命令 附录1.1 管理用命令 函数名 功能描 ...

  6. MATLAB函数汇总

    Matlab 函数参数汇总 MATLAB函数参考 附录1.1 管理用命令 函数名 功能描述 函数名 功能描述 addpath 增加一条搜索路径 rmpath 删除一条搜索路径 demo 运行Matla ...

  7. MATLAB 函数大全

    附录1 常用命令 附录1.1 管理用命令 函数名 功能描述 函数名 功能描述 addpath 增加一条搜索路径 rmpath 删除一条搜索路径 demo 运行Matlab演示程序 type 列出.M文 ...

  8. Matlab常用命令汇总

    1 管理命令 1.1 管理用命令 函数名 功能描述 函数名 功能描述 addpath 增加一条搜索路径 rmpath 删除一条搜索路径 demo 运行Matlab演示程序 type 列出.M文件 do ...

  9. Matlab学习-图像处理工具箱函数

    本文转载自http://www.cnblogs.com/gtts/archive/2011/05/20/2052339.html 下列表格中除了个别函数外,其余函数都是图像处理工具箱提供的关于图像处理 ...

最新文章

  1. JavaScript 高级技巧
  2. coreutils-5.0中几个命令的执行过程
  3. shrio初体验(2)Realm
  4. 作者:丁铖(1992-),男,华东师范大学计算机科学与软件工程学院硕士生。...
  5. mysql 密码eba_MySQL-体系结构及授权管理
  6. c语言状态机_【C语言】有限状态机FSM
  7. jquery新版本旧版本之间的坑
  8. Apache Spark技术实战之5 -- SparkR的安装及使用
  9. Unity 制作虚拟手柄例子
  10. 单片机c51交通灯c语言程序,c51单片机交通灯程序
  11. LayoutInflater解析
  12. mysql主从配置(超简单)
  13. niushop多商户商户端手机uniapp源码v4单商户v4_Saas开源版含uniapp以及niushop社区团购标准版源码开源的区别
  14. Android多开分身 v7.2 破解永久VIP付费版
  15. 计算机二级vbf课百度云,计算机二级易错易混选择题.
  16. 【100%通过率】华为OD机试真题 Java 实现【猜字谜】【2022.11 Q4 新题】
  17. python智能写作_闲人AI写作智能文章生成-文章伪原创-关键词生成文章工具
  18. Android 集成QQ登录,获取头像与昵称
  19. 求用户最大连续登陆天数mysql实现
  20. vue-router之解决addRoutes使用遇到的坑

热门文章

  1. 中国资源卫星应用中心新版数据服务平台介绍
  2. 全球最厉害的14位程序员你都认识谁
  3. WLAN技术入门(一):WLAN技术发展简史
  4. 医疗汇报医学演示PPT模板
  5. c语言中215 10等于,云南铜业高级技工学校2014-215学年第一学期《电气控制与PLC》期中考试试卷B卷(答案)...
  6. ❤ 就这?TypeScript其实并不难!(建议收藏)❤
  7. 【渝粤题库】陕西师范大学165110 培训管理 作业(高起专)
  8. 部分Windows 10 LTSC 2021版用户收到22H2版更新 目测又是微软BUG
  9. c语言最大数最小数平均数,C语言编程 求两个数的平均值方法(三种方法)
  10. pde与波长 sipm 关系_硅光电倍增管