方波的离散傅立叶变换

时间:2018-11-27 21:21:08

标签: python numpy matplotlib sympy dft

这是我的第一篇文章,所以请您原谅我的任何错误。 一个学校项目让我计算了红外波的离散傅立叶变换。我必须使用Python 3.xx中的sympy,numpy和matplot库来执行此操作,但是我不能使用已经内置的fft算法。

我使用的公式:

尽管我可能在理论或程序上有些失落,但我认为我没有得到正确的结果,因为我希望在0.5Hz处出现一个峰值(请参见下图)。我使用了维基百科的公式(见下文)。

由FourierTransform生成的图形:

相同的图形,但放大至0.5:

非常详细的缩放,大约为0.5:

我们测得的波浪的可视化

请在下面评估我的代码

import sympy as sp
import numpy as np
import matplotlib.pyplot as plt

def FourierTransform(y):
    N = len(y) #We have N samples
    j = 1j      #The complex number j (or i depending on your region)
    Fy = []
    for k in range(N):
        XK = 0
        for n in range(N):
            bo = -j*np.pi*2*n*k/N #see formula supplied in post, we split it here for easier formating (otherwise to many brackets)
            XK += y[n]*np.exp(bo)
        Fy.append(XK)
    plt.figure()
    plt.plot(range(N),Fy)
    plt.title('Discrete Fourier Transform')
    plt.xlabel('Frequency')
    plt.ylabel('Magnitude')
    return Fy

def main():
    ###Y are the values we measured, corresponding to the time in milliseconds (X)
    Y = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    X = [0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 420, 440, 460, 480, 500, 520, 540, 560, 580, 600, 620, 640, 660, 680, 700, 720, 740, 760, 780, 800, 820, 840, 860, 880, 900, 920, 940, 960, 980, 1000, 1020, 1040, 1060, 1080, 1100, 1120, 1140, 1160, 1180, 1200, 1220, 1240, 1260, 1280, 1300, 1320, 1340, 1360, 1380, 1400, 1420, 1440, 1460, 1480, 1500, 1520, 1540, 1560, 1580, 1600, 1620, 1640, 1660, 1680, 1700, 1720, 1740, 1760, 1780, 1800, 1820, 1840, 1860, 1880, 1900, 1920, 1940, 1960, 1980, 2000, 2020, 2040, 2060, 2080, 2100, 2120, 2140, 2160, 2180, 2200, 2220, 2240, 2260, 2280, 2300, 2320, 2340, 2360, 2380, 2400, 2420, 2440, 2460, 2480, 2500, 2520, 2540, 2560, 2580, 2600, 2620, 2640, 2660, 2680, 2700, 2720, 2740, 2760, 2780, 2800, 2820, 2840, 2860, 2880, 2900, 2920, 2940, 2960, 2980, 3000, 3020, 3040, 3060, 3080, 3100, 3120, 3140, 3160, 3180, 3200, 3220, 3240, 3260, 3280, 3300, 3320, 3340, 3360, 3380, 3400, 3420, 3440, 3460, 3480, 3500, 3520, 3540, 3560, 3580, 3600, 3620, 3640, 3660, 3680, 3700, 3720, 3740, 3760, 3780, 3800, 3820, 3840, 3860, 3880, 3900, 3920, 3940, 3960, 3980, 4000, 4020, 4040, 4060, 4080, 4100, 4120, 4140, 4160, 4180, 4200, 4220, 4240, 4260, 4280, 4300, 4320, 4340, 4360, 4380, 4400, 4420, 4440, 4460, 4480, 4500, 4520, 4540, 4560, 4580, 4600, 4620, 4640, 4660, 4680, 4700, 4720, 4740, 4760, 4780, 4800, 4820, 4840, 4860, 4880, 4900, 4920, 4940, 4960, 4980, 5000, 5020, 5040, 5060, 5080, 5100, 5120, 5140, 5160, 5180, 5200, 5220, 5240, 5260, 5280, 5300, 5320, 5340, 5360, 5380, 5400, 5420, 5440, 5460, 5480, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700, 5720, 5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880, 5900, 5920, 5940, 5960, 5980, 6000, 6020, 6040, 6060, 6080, 6100, 6120, 6140, 6160, 6180, 6200, 6220, 6240, 6260, 6280, 6300, 6320, 6340, 6360, 6380, 6400, 6420, 6440, 6460, 6480, 6500, 6520, 6540, 6560, 6580, 6600, 6620, 6640, 6660, 6680, 6700, 6720, 6740, 6760, 6780, 6800, 6820, 6840, 6860, 6880, 6900, 6920, 6940, 6960, 6980, 7000, 7020, 7040, 7060, 7080, 7100, 7120, 7140, 7160, 7180, 7200, 7220, 7240, 7260, 7280, 7300, 7320, 7340, 7360, 7380, 7400, 7420, 7440, 7460, 7480, 7500, 7520, 7540, 7560, 7580, 7600, 7620, 7640, 7660, 7680, 7700, 7720, 7740, 7760, 7780, 7800, 7820, 7840, 7860, 7880, 7900, 7920, 7940, 7960, 7980, 8000, 8020, 8040, 8060, 8080, 8100, 8120, 8140, 8160, 8180, 8200, 8220, 8240, 8260, 8280, 8300, 8320, 8340, 8360, 8380, 8400, 8420, 8440, 8460, 8480, 8500, 8520, 8540, 8560, 8580, 8600, 8620, 8640, 8660, 8680, 8700, 8720, 8740, 8760, 8780, 8800, 8820, 8840, 8860, 8880, 8900, 8920, 8940, 8960, 8980, 9000, 9020, 9040, 9060, 9080, 9100, 9120, 9140, 9160, 9180, 9200, 9220, 9240, 9260, 9280, 9300, 9320, 9340, 9360, 9380, 9400, 9420, 9440, 9460, 9480, 9500, 9520, 9540, 9560, 9580, 9600, 9620, 9640, 9660, 9680, 9700, 9720, 9740, 9760, 9780, 9800, 9820, 9840, 9860, 9880, 9900, 9920, 9940, 9960, 9980, 10000, 10020, 10040, 10060, 10080, 10100, 10120, 10140, 10160, 10180, 10200, 10220, 10240, 10260, 10280, 10300, 10320, 10340, 10360, 10380, 10400, 10420, 10440, 10460, 10480, 10500, 10520, 10540, 10560, 10580, 10600, 10620, 10640, 10660, 10680, 10700, 10720, 10740, 10760, 10780, 10800, 10820, 10840, 10860, 10880, 10900, 10920, 10940, 10960, 10980, 11000, 11020, 11040, 11060, 11080, 11100, 11120, 11140, 11160, 11180, 11200, 11220, 11240, 11260, 11280, 11300, 11320, 11340, 11360, 11380, 11400, 11420, 11440, 11460, 11480, 11500, 11520, 11540, 11560, 11580, 11600, 11620, 11640, 11660, 11680, 11700, 11720, 11740, 11760, 11780, 11800, 11820, 11840, 11860, 11880, 11900, 11920, 11940, 11960, 11980, 12000, 12020, 12040, 12060, 12080, 12100, 12120, 12140, 12160, 12180, 12200, 12220, 12240, 12260, 12280, 12300, 12320, 12340, 12360, 12380, 12400, 12420, 12440, 12460, 12480, 12500, 12520, 12540, 12560, 12580, 12600, 12620, 12640, 12660, 12680, 12700, 12720, 12740, 12760, 12780, 12800, 12820, 12840, 12860, 12880, 12900, 12920, 12940, 12960, 12980, 13000, 13020, 13040, 13060, 13080, 13100, 13120, 13140, 13160, 13180, 13200, 13220, 13240, 13260, 13280, 13300, 13320, 13340, 13360, 13380, 13400, 13420, 13440, 13460, 13480, 13500, 13520, 13540, 13560, 13580, 13600, 13620, 13640, 13660, 13680, 13700, 13720, 13740, 13760, 13780, 13800, 13820, 13840, 13860, 13880, 13900, 13920, 13940, 13960, 13980, 14000, 14020, 14040, 14060, 14080, 14100, 14120, 14140, 14160, 14180, 14200, 14220, 14240, 14260, 14280, 14300, 14320, 14340, 14360, 14380, 14400, 14420, 14440, 14460, 14480, 14500, 14520, 14540, 14560, 14580, 14600, 14620, 14640, 14660, 14680, 14700, 14720, 14740, 14760, 14780, 14800, 14820, 14840, 14860, 14880, 14900, 14920, 14940, 14960, 14980, 15000, 15020, 15040, 15060, 15080, 15100, 15120, 15140, 15160, 15180, 15200, 15220, 15240, 15260, 15280, 15300, 15320, 15340, 15360, 15380, 15400, 15420, 15440, 15460, 15480, 15500, 15520, 15540, 15560, 15580, 15600, 15620, 15640, 15660, 15680, 15700, 15720, 15740, 15760, 15780, 15800, 15820, 15840, 15860, 15880, 15900, 15920, 15940, 15960, 15980, 16000, 16020, 16040, 16060, 16080, 16100, 16120, 16140, 16160, 16180, 16200, 16220, 16240, 16260, 16280, 16300, 16320, 16340, 16360, 16380, 16400, 16420, 16440, 16460, 16480, 16500, 16520, 16540, 16560, 16580, 16600, 16620, 16640, 16660, 16680, 16700, 16720, 16740, 16760, 16780, 16800, 16820, 16840, 16860, 16880, 16900, 16920, 16940, 16960, 16980, 17000, 17020, 17040, 17060, 17080, 17100, 17120, 17140, 17160, 17180, 17200, 17220, 17240, 17260, 17280, 17300, 17320, 17340, 17360, 17380, 17400, 17420, 17440, 17460, 17480, 17500, 17520, 17540, 17560, 17580, 17600, 17620, 17640, 17660, 17680, 17700, 17720, 17740, 17760, 17780, 17800, 17820, 17840, 17860, 17880, 17900, 17920, 17940, 17960, 17980, 18000, 18020, 18040, 18060, 18080, 18100, 18120, 18140, 18160, 18180, 18200, 18220, 18240, 18260, 18280, 18300, 18320, 18340, 18360, 18380, 18400, 18420, 18440, 18460, 18480, 18500, 18520, 18540, 18560, 18580, 18600, 18620, 18640, 18660, 18680, 18700, 18720, 18740, 18760, 18780, 18800, 18820, 18840, 18860]
    FourierTransform(Y)

main()

我非常感谢您给出的所有答案,因为该程序一直使我彻夜难眠。

谢谢!

0 个答案:

没有答案