• 【Python】Numpy傅里叶变换总结


    文章目录

    简介

    Fourier变换极其逆变换在数学上的定义如下

    F ( ω ) = ∫ − ∞ ∞ f ( t ) e − i ω t d t f ( t ) = π 2 ∫ − ∞ ∞ F ( ω ) e i ω t d ω F(\omega)=\int^\infty_{-\infty}f(t)e^{-i\omega t}\text dt\\ f(t)=\frac{\pi}{2}\int^\infty_{-\infty}F(\omega)e^{i\omega t}\text d\omega F(ω)=f(t)etdtf(t)=2πF(ω)etdω

    numpy中封装了快速Fourier算法,即FFT,根据数值范围、正反变换以及数组维度,共有下表这些函数可供使用,其前缀i表示逆变换。

    一维二维n维
    标准fft, ifftfft2, ifft2fftn, ifftn
    实数rfft, irfftrfft2, irfft2rfftn, rfftn
    厄米hfft, ihfft

    此外,还有获取频率和相位的两组函数fftfreq, rfftfreq, fftshift, ifftshift

    fft

    下面初步了解一下fft的功能

    import numpy as np
    import matplotlib.pyplot as plt
    from numpy.fft import *
    t = np.arange(256)
    sp = fft(np.sin(t))
    freq = fftfreq(t.shape[-1])
    plt.plot(freq, sp.real, freq, sp.imag)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    效果如下,表明Fourier变换后,其实部关于 y y y轴对称,而虚部呈中心对称,或者说关于 y y y轴呈反对称。

    在这里插入图片描述

    由于Fourier变换完成的是时空到频率的转换,而频率往往是对事物特征的某种刻画,换言之,保留部分最强的频率,也就保留了事物最重要的特征,故而Fourier常被用于图像滤波和压缩等领域。

    import numpy as np
    import matplotlib.pyplot as plt
    from numpy.fft import *
    img = plt.imread('test.jpg').astype(float)
    f = fft(img)
    
    fig = plt.figure()
    ax = fig.add_subplot(221)
    ax.imshow(np.log(np.abs(f.real)+0.1))     #因为f.real太大
    ax.axis('off')
    clrs = ['Reds', 'Greens', 'Blues']
    for i in range(3):
        ax = fig.add_subplot(2,2,i+2)
        ax.imshow(np.log(np.abs(f[:,:,i].real)+0.1), cmap=clrs[i])
        ax.axis('off')
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    从而得到下图的样子,其中后面三张分别为R, G, B三个分量的傅里叶变换的实部

    在这里插入图片描述

    所以从频域上几乎什么都看不出来,除非用傅里叶逆变换将其复原

    img1 = ifft2(f)
    plt.imshow(np.real(img1)/255)
    plt.axis('off')
    
    • 1
    • 2
    • 3

    结果为

    在这里插入图片描述

  • 相关阅读:
    微信小程序|自定义弹窗组件
    OPPO主题组件开发 - 组件内容自适应
    csgo 闪退
    闵帆老师《论文写作》课学习心得
    Java+SpringBoot+Vue+MySQL:农业管理新篇章
    【C#语言】DataGridView隔行显示颜色
    AMBA:AXI/AHB/APB学习笔记
    记:lorawan协议
    leetcode困难题
    MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
  • 原文地址:https://blog.csdn.net/m0_37816922/article/details/127973833