Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calculate amplitude, phase and energy spectrum using FFT in MATLAB

  1. Jul 17, 2012 #1
    Hello all,

    I have a vector, lets say A, which has measured values of the Earth's magnetic field (and if it is relevant, two vectors X and Y with the coordinates of the point each value was measured). I need to calculate the amplitude, phase and energy spectrum using FFT in MATLAB
    Code (Text):
         X            Y        A
    399845.66 399018.66 -3.52
    399750.69 399180.44 -3.587
    399656 399342.19 -3.653
    399561.63 399504.06 -3.721
    399467.22 399665.91 -3.788
    399372.66 399827.78 -3.849
    399277.88 399989.56 -3.906
    399183.19 400151.13 -3.955
    399088.22 400312.34 -3.997
    398992.91 400473.5 -4.031
    398897.25 400634.75 -4.058
    398801.44 400796.5 -4.079
    398705.91 400958.53 -4.095
    398610.44 401120.91 -4.104
    398515.13 401283.5 -4.107
    398420 401446.63 -4.106
    398325.16 401610.53 -4.1
    398230.88 401774.84 -4.089
    398136.81 401938.47 -4.069
    398042.25 402101.34 -4.038
    397946.97 402263.63 -3.997
    397850.97 402425.38 -3.948
    397754.88 402587 -3.891
    397658.81 402748.88 -3.829
    397563.44 402911.34 -3.765
    397469.19 403074.34 -3.696
    397375.5 403236.94 -3.621
    397281.69 403399.25 -3.538
    397187.28 403561.13 -3.447
    397092.38 403722.84 -3.348
    396997 403884.66 -3.243
    396901.53 404046.5 -3.132
    396806.19 404208.66 -3.016
    396711.06 404371.03 -2.895
    396616.28 404533.38 -2.768
    396521.53 404695.53 -2.633
    396426.38 404857.22 -2.49
    396330.91 405018.84 -2.34
    396235.25 405180.5 -2.183
    396139.5 405342.13 -2.018
    396043.78 405503.97 -1.847
    395948.34 405665.84 -1.668
    395853 405827.56 -1.482
    395757.66 405989.41 -1.287
    395662.63 406151.22 -1.084
    395567.94 406312.91 -0.873
    395473.59 406474.59 -0.652
    395379.41 406635.94 -0.421
    395285.59 406797.25 -0.179
    395192 406958.78 0.072
    395098.44 407120.53 0.332
    395004.69 407282.63 0.603
    394910.78 407444.94 0.884
    394816.66 407607.16 1.178
    394722.13 407768.97 1.485
    394627.09 407930.34 1.806
    394531.66 408091.44 2.142
    394436.13 408252.41 2.49
    394340.59 408413.5 2.851
    394245.38 408574.69 3.226
    394150.25 408735.88 3.614
    394055.38 408896.91 4.018
    393960.75 409057.84 4.438
    393865.88 409218.56 4.874
    393771.06 409379.41 5.327
    393675.91 409540.38 5.798
    393580.91 409701.44 6.287
    393485.78 409862.41 6.796
    393390.88 410023.25 7.323
    393296.13 410183.97 7.869
    393201.69 410344.88 8.435
    393107.66 410506 9.021
    393013.94 410667.34 9.626
    392920.41 410828.88 10.252
    392827 410990.66 10.899
    392733.63 411152.63 11.568
    392639.75 411314.31 12.261
    392545.16 411475.91 12.98
    392449.81 411637.25 13.725
    392353.66 411798.16 14.497
    392257.31 411958.88 15.298
    392160.94 412119.38 16.127
    392064.88 412279.94 16.984
    391969.41 412440.72 17.87
    391874.56 412602.03 18.785
    391780.41 412763.84 19.73
    391686.88 412926.28 20.707
    391593.66 413088.81 21.72
    391500.13 413250.94 22.773
    391405.97 413412.53 23.871
    391311.22 413573.5 25.015
    391216.03 413733.81 26.207
    391120.75 413893.81 27.447
    391025.47 414053.91 28.737
    390930.59 414214.34 30.077
    390836.22 414375.19 31.466
    390742.56 414536.31 32.907
    390649.22 414697.97 34.4
    390556 414859.88 35.946
    390462.69 415021.88 37.549
    390368.5 415183.91 39.221
    390273.53 415345.56 40.96
    390177.84 415506.91 42.76
    390081.69 415668.09 44.612
    389985.53 415829.03 46.509
    389889.53 415989.63 48.441
    389794.13 416150.22 50.395
    389699.38 416310.88 52.362
    389605.56 416471.69 54.326
    389512.59 416632.66 56.276
    389420.13 416793.97 58.197
    389327.78 416955.25 60.075
    389235.16 417116.56 61.896
    389141.81 417277.47 63.644
    389047.88 417437.97 65.305
    388953.53 417597.81 66.864
    388858.88 417757.38 68.315
    388764.38 417916.88 69.644
    388670.13 418076.41 70.838
    388575.88 418236.16 71.883
    388481.72 418396.13 72.768
    388387.56 418556.06 73.481
    388293.34 418716.16 74.012
    388198.91 418876.25 74.349
    388104.41 419036.34 74.482
    388009.53 419196.44 74.401
    387914.72 419356.44 74.096
    387819.91 419516.44 73.56
    387725.22 419676.56 72.784
    387630.44 419836.78 71.76
    387535.66 419997.22 70.483
    387441.31 420157.66 68.948
    387347.31 420318.19 67.146
    387253.63 420478.56 65.075
    387159.75 420638.97 62.738
    387065.88 420799.38 60.137
    386971.78 420959.66 57.281
    386877.53 421120.09 54.178
    386783.16 421280.31 50.841
    386688.88 421440.41 47.284
    386594.78 421600.25 43.526
    386500.88 421760.13 39.585
    386407.31 421920.28 35.482
    386314.06 422080.88 31.241
    386220.97 422241.59 26.886
    386128.13 422402.38 22.445
    386035.16 422563.06 17.944
    385941.81 422723.66 13.412
    385847.97 422884.19 8.923
    385753.38 423044.53 4.505
    385658.25 423204.59 0.171
    385562.75 423364.53 -4.069
    385467.03 423524.28 -8.204
    385371.22 423683.91 -12.229
    385275.72 423843.88 -16.137
    385180.72 424004.03 -19.925
    385086.41 424164.38 -23.591
    384992.5 424324.91 -27.132
    384899.13 424485.84 -30.55
    384806.13 424647 -33.845
    384713.34 424808.38 -37.019
    384620.5 424970.16 -40.076
    384527.44 425132 -43.019
    384433.91 425293.72 -45.849
    384339.63 425455.19 -48.524
    384244.63 425616.44 -51.023
    384149.13 425777.53 -53.343
    384053.41 425938.5 -55.481
    383957.84 426099.59 -57.437
    383862.34 426260.69 -59.209
    383767.25 426421.84 -60.799
    383672.28 426583 -62.209
    383577.78 426744.38 -63.443
    383483.5 426906.06 -64.507
    383389.44 427067.72 -65.405
    383295.25 427229.41 -66.144
    383201.16 427390.84 -66.733
    383106.81 427552.31 -67.18
    383012.34 427713.91 -67.495
    382917.59 427875.38 -67.688
    382822.53 428036.91 -67.781
    382727.31 428198.31 -67.786
    382631.78 428359.53 -67.715
    382536.13 428520.63 -67.577
    382440.53 428681.94 -67.382
    382345.31 428843.47 -67.138
    382250.5 429005.19 -66.851
    382156.38 429167.09 -66.529
    382062.63 429329.22 -66.175
    381969.13 429491.31 -65.793
    381875.5 429653.41 -65.388
    381781.28 429815.22 -64.96
    381686.47 429976.63 -64.512
    381591.25 430137.81 -64.047
    381495.75 430298.81 -63.567
    381400.31 430459.69 -63.076
    381305.09 430620.56 -62.58
    381210.09 430781.53 -62.082
    381115.66 430942.69 -61.586
    381021.53 431104.03 -61.091
    380927.53 431265.72 -60.6
    380833.34 431427.44 -60.111
    380738.97 431589.25 -59.625
    380644.22 431751.22 -59.141
    380549.19 431913.09 -58.658
    380453.63 432074.66 -58.175
    380357.63 432236.34 -57.691
    380261.72 432398.06 -57.207
    380165.91 432559.84 -56.722
    380070.59 432721.84 -56.236
    379975.69 432884.16 -55.749
    379881.28 433046.88 -55.259
    379787.19 433209.69 -54.766
    379693.31 433372.72 -54.269
    379599.34 433535.75 -53.768
    379504.88 433698.59 -53.261
    379409.91 433861.38 -52.747
    379314.25 434023.72 -52.227
    379218.13 434185.78 -51.702
    379121.47 434347.75 -51.172
    379024.53 434509.5 -50.637
    378927.5 434671.28 -50.099
    378830.75 434832.94 -49.56
    378734.31 434994.66 -49.019
    378638.38 435156.72 -48.479
    378542.94 435318.97 -47.94
    378448 435481.63 -47.403
    378353.47 435644.59 -46.869
    378259.06 435807.56 -46.336
    378164.53 435970.53 -45.803
    378070.03 436133.5 -45.274
    377975.34 436296.69 -44.748
    377880.38 436460.03 -44.227
    377785.41 436623.47 -43.712
    377690.25 436786.91 -43.204
    377595 436950.38 -42.705
    377499.69 437114.19 -42.215
    377404.28 437278.09 -41.735
    377308.88 437442 -41.267
    377213.41 437606.06 -40.811
    377117.53 437770.13 -40.365
    377021.25 437933.97 -39.931
    376924.5 438097.75 -39.508
    376827.47 438261.53 -39.096
    376730.25 438425.22 -38.694
    376633 438588.94 -38.303
    376535.78 438752.75 -37.921
    376438.88 438916.63 -37.547
    376342.03 439080.53 -37.181
    376245.72 439244.63 -36.822
    376149.78 439408.94 -36.469
    376054.16 439573.09 -36.12
    375958.5 439737.16 -35.773
    375862.97 439901.19 -35.429
    375767.44 440065.13 -35.086
    375671.88 440229.09 -34.743
    375576.34 440392.91 -34.401
    375480.63 440556.88 -34.059
    375384.69 440720.84 -33.718
    375288.69 440884.91 -33.377
    375192.5 441049.13 -33.038
    375096.22 441213.44 -32.702
    374999.69 441378 -32.368
    374903.06 441542.81 -32.037
    374806.16 441707.72 -31.709
    374709.06 441872.88 -31.385
    374611.81 442038.13 -31.066
    374514.06 442203.66 -30.751
    374416.53 442368.97 -30.439
    374318.88 442534.25 -30.132
    374221.22 442699.44 -29.828
    374123.5 442864.66 -29.528
    374025.84 443029.84 -29.232
    373928.22 443195.03 -28.939
    373830.59 443360.34 -28.649
    373733.06 443525.63 -28.361
    373635.44 443691.06 -28.076
    373537.91 443856.34 -27.792
    373440.59 444021.75 -27.51
    373343.47 444187.03 -27.229
    373246.53 444352.31 -26.949
    373150 444517.75 -26.671
    373053.78 444683.56 -26.396
    372957.88 444849.66 -26.124
    372861.97 445015.88 -25.855
    372765.91 445182.19 -25.587
    372669.13 445348.47 -25.32
    372571.59 445514.44 -25.053
    372473.38 445680.13 -24.787
    372374.63 445845.63 -24.52
    372275.5 446010.91 -24.255
    372176.5 446176.31 -23.992
    372077.97 446341.69 -23.732
    371979.72 446507.28 -23.475
    371882.13 446673.25 -23.222
    371785 446839.44 -22.973
    371688.19 447006.06 -22.727
    371591.5 447172.78 -22.483
    371494.59 447339.38 -22.239
    371397.19 447505.59 -21.995
    371299.31 447671.5 -21.749
    371200.91 447836.97 -21.502
    371102.38 448002.13 -21.253
    371004.16 448167.16 -21.004
    370906.03 448332.19 -20.754
    370808.38 448497.22 -20.503
    370711.44 448662.28 -20.251
    370614.88 448827.56 -19.999
    370518.81 448992.91 -19.747
    370423.06 449158.47 -19.494
    370327.5 449324.25 -19.24
    370232.25 449490.13 -18.985
    370136.81 449656 -18.728
    370041.06 449821.56 -18.468
    369944.72 449986.72 -18.204
    369847.66 450151.47 -17.936
    369749.91 450315.94 -17.665
    369652 450480.19 -17.39
    369554.03 450644.25 -17.113
    369456.38 450808.13 -16.832
    369358.91 450971.94 -16.548
    369262 451135.69 -16.26
    369165.41 451299.66 -15.968
    369069.53 451463.78 -15.67
    368974.03 451628.03 -15.368
    368879.03 451792.44 -15.058
    368784.41 451957.06 -14.743
    368690.03 452121.91 -14.421
    368595.34 452286.75 -14.092
    368500.16 452451.31 -13.754
    368404.38 452615.44 -13.407
    368308.03 452779.41 -13.053
    368211.25 452943.16 -12.693
    368114.22 453107.16 -12.331
    368017.31 453271.38 -11.966
    367920.19 453435.47 -11.601
    367823.09 453599.81 -11.236
    367725.91 453764.38 -10.873
    367628.91 453928.84 -10.512
    367532.03 454093.5 -10.155
    367435.38 454258.38 -9.804
    367338.91 454423.34 -9.46
    367242.44 454588.41 -9.122
    367145.88 454753.5 -8.794
    367048.84 454918.38 -8.474
    366951.41 455083.09 -8.166
    366853.78 455247.59 -7.871
    366756.31 455411.94 -7.594
    366659.06 455576.31 -7.337
    366562.13 455740.75 -7.104
    366465.75 455905.5 -6.899
    366369.88 456070.47 -6.725
    366274.38 456235.84 -6.586
    366179.03 456401.53 -6.485
    366083.53 456567.34 -6.423
    365987.72 456733.06 -6.401
    365891.53 456898.38 -6.422
    365794.72 457063.25 -6.485
    365697.59 457227.72 -6.593
    365599.97 457391.91 -6.745
    365501.94 457555.75 -6.943
    365403.72 457719.41 -7.185
    365305.69 457882.94 -7.472
    365208.25 458046.56 -7.802
    365111.59 458210.34 -8.176
    365015.72 458374.53 -8.59
    364920.38 458539.13 -9.042
    364825.34 458704.13 -9.529
    364730.31 458869.16 -10.046
    364634.81 459034.22 -10.59
    364538.81 459198.75 -11.157
    364442.19 459362.88 -11.743
    364345.47 459526.66 -12.344
    364248.63 459690.25 -12.957
    364151.81 459854.06 -13.578
    364055.06 460017.75 -14.202
    363958.25 460181.31 -14.826
    363861.44 460345.03 -15.444
    363764.63 460508.94 -16.054
    363667.94 460672.84 -16.652
    363571.22 460836.75 -17.235
    363474.53 461000.56 -17.8
    363378.25 461164.69 -18.348
    363282.44 461329 -18.877
    363187.06 461493.5 -19.388
    363091.97 461658.31 -19.881
    362996.78 461823.25 -20.359
    362901.25 461988.13 -20.822
    362804.88 462152.66 -21.269
    362707.84 462316.72 -21.703
    362610.38 462480.47 -22.125
    362512.84 462644 -22.539
    362415.28 462807.66 -22.945
    362317.84 462971.41 -23.346
    362220.81 463135.47 -23.742
    362124.31 463299.94 -24.132
    362028.13 463465.06 -24.518
    361932.44 463630.91 -24.897
    361837.03 463797.22 -25.266
    361741.78 463963.5 -25.621
    361646.53 464129.59 -25.96
    361550.75 464295.13 -26.276
    361454.38 464460.16 -26.567
    361357.59 464624.41 -26.832
    361260.41 464788.38 -27.069
    361163 464952.38 -27.28
    361065.72 465116.34 -27.464
    360968.56 465280.66 -27.621
    360871.59 465445.06 -27.75
    360774.63 465609.69 -27.852
    360677.72 465774.63 -27.926
    360580.88 465939.69 -27.974
    360483.88 466105 -27.996
    360386.78 466270.09 -27.991
    360289.88 466435.16 -27.962
    360192.94 466599.88 -27.91
    360096.13 466764.75 -27.837
    359999.41 466929.59 -27.743
    359903 467094.63 -27.632
    359806.5 467259.91 -27.506
    359710.13 467425.38 -27.367
    359613.47 467590.88 -27.217
    359516.78 467756.28 -27.058
    359420.13 467921.47 -26.894
    359323.41 468086.22 -26.726
    359226.88 468250.94 -26.558
    359130.41 468415.78 -26.393
    359034 468580.72 -26.234
    358937.53 468745.66 -26.081
    358840.84 468910.63 -25.937
    358744.19 469075.81 -25.802
    358647.44 469240.88 -25.677
    358550.78 469406.19 -25.562
    358453.84 469571.38 -25.458
    358356.81 469736.38 -25.363
    358259.56 469901.28 -25.276
    358162.13 470065.72 -25.196
    358064.66 470230.09 -25.123
    357967.19 470394.31 -25.057
    357869.84 470558.78 -24.997
    357772.5 470723.47 -24.943
    357675.22 470888.69 -24.894
    357577.56 471054.28 -24.848
    357479.91 471220.09 -24.803
    357382.19 471386.03 -24.758
    357284.75 471551.94 -24.712
    357187.91 471717.81 -24.664
    357091.44 471883.78 -24.613
    356995.47 472049.81 -24.558
    356899.53 472216.22 -24.499
    356803.31 472382.94 -24.435
    356706.56 472549.94 -24.364
    356609.09 472716.84 -24.287
    356510.94 472883.38 -24.2
    356412.41 473049.47 -24.105
    356313.53 473215.25 -24.003
    356214.88 473380.69 -23.895
    356116.56 473546.13 -23.783
    356018.94 473711.63 -23.668
    355921.84 473877.41 -23.554
    355825.06 474043.5 -23.44
    355728.66 474210.03 -23.327
    355632.19 474376.59 -23.216
    355535.53 474543.03 -23.104
    355438.47 474709.25 -22.99
    355340.88 474875.09 -22.876
    355246.59 475034.41 -22.768
    355189.84 475129.84 -22.704
    355091.66 475295.16 -22.598
    354993.63 475460.69 -22.5
    354896.06 475626.41 -22.414
    354799.19 475792.41 -22.342
    354702.75 475958.88 -22.287
    354606.41 476125.5 -22.253
    354509.88 476292.41 -22.24
    354413 476459.31 -22.252
    354315.59 476625.91 -22.29
    354217.53 476792.34 -22.358
    354119.13 476958.22 -22.458
    354020.34 477123.94 -22.588
    353921.66 477289.41 -22.75
    353823.03 477454.88 -22.944
    353725.03 477620.53 -23.175
    353627.53 477786.47 -23.443
    353530.66 477953.06 -23.751
    353434.19 478120.16 -24.097
    353337.84 478287.72 -24.481
    353241.31 478455.41 -24.897
    353144.41 478622.78 -25.344
    353046.78 478789.84 -25.817
    352948.59 478956.63 -26.314
    352849.97 479123 -26.83
    352751.44 479289.03 -27.364
    352653.28 479454.91 -27.912
    352555.72 479620.56 -28.47
    352458.72 479786.28 -29.031
    352362.22 479952.5 -29.59
    352265.88 480119.19 -30.14
    352169.44 480286.09 -30.676
    352072.63 480453.25 -31.193
    351975.25 480620.22 -31.683
    351877.25 480786.78 -32.143
    351778.84 480952.69 -32.567
    351680.31 481118.16 -32.953
    351581.88 481283.22 -33.299
    351483.5 481448.13 -33.604
    351385.34 481613.25 -33.866
    351287.38 481778.63 -34.084
    351189.44 481943.94 -34.258
    351091.53 482109.5 -34.387
    350993.69 482275.06 -34.474
    350896.16 482440.72 -34.521
    350798.81 482606.16 -34.532
    350701.56 482771.56 -34.51
    350604.53 482937.09 -34.46
    350507.5 483102.72 -34.388
    350410.56 483268.34 -34.297
    350313.63 483434.06 -34.195
    350216.91 483599.91 -34.087
    350120.09 483765.84 -33.978
    350023.19 483931.81 -33.873
    349911.16 484123.34 -33.764
    349814.19 484289.41 -33.683
     
    I try to do it like this.
    First I load the data in to a vector A, then:
    Code (Text):

    L=numel(A);
    Nfft=2^nextpow2(L);
    amp=abs(fft(A,Nfft))/Nfft;
    pha=angle(fft(A,Nfft));
    ene=amp.^2;
    enl=2.*log(amp);
     
    But it doesn't seem to work properly, because i have the 513th values of each spectrum
    for the amplitude it's 0.001935, for phase 0, for power 0.000004 and for log(energy) -12.495
     
  2. jcsd
  3. Jul 18, 2012 #2

    chiro

    User Avatar
    Science Advisor

    I've noticed that you use ^ and / instead of .^ and ./ but you use the dot operator for the other commands. Is there a reason for this?
     
  4. Jul 18, 2012 #3
    Yes there is a reason, ./ and .* are used when handling vectors and matrices and * and / are used when handling scalars
     
  5. Jul 18, 2012 #4

    chiro

    User Avatar
    Science Advisor

    What about the vector A? How did you load it in the MATLAB environment? Did you use a routine to load it from a file or is completely generated within the MATLAB environment itself?

    This might seem pedantic, but if its loaded externally maybe you should print off the vector just to make sure it has the right values.
     
  6. Jul 18, 2012 #5
    The data is loaded from an external file, but i've already checked if it is correct.
    Thanks anyway :)
     
  7. Jul 18, 2012 #6

    chiro

    User Avatar
    Science Advisor

    Maybe you could just print out the values of L and Nfft in MATLAB just to verify you get the right values for your FFT.
     
  8. Jul 18, 2012 #7
    I've checked the values L=523 and Nfft=1024
     
  9. Jul 18, 2012 #8

    chiro

    User Avatar
    Science Advisor

    One final question: Do you have to normalize your amplitude (divide by Nfft) or does fft do it for you?
     
  10. Jul 18, 2012 #9
    Yes i have normalized the results
     
  11. Jul 18, 2012 #10

    chiro

    User Avatar
    Science Advisor

    No, I meant by asking the question is "Do you have to normalize them yourself, or does the fft do this for you?"

    The reason I ask is because in MATLAB it does do a 1/N calculation:

    http://www.mathworks.com.au/help/techdoc/ref/fft.html
     
  12. Jul 18, 2012 #11
    I have normalized them by myself, if I don't I get very large values.
     
  13. Jul 18, 2012 #12

    chiro

    User Avatar
    Science Advisor

    Well the only I got left to ask is if you have another data set to be used as a reference set to check the answers?

    At least if you have another reference set, you can check if the values calculated match was is output. At least if you get an error here, then you can use a different routine.

    One other suggestion is to re-construct the signal to see what the output gives.

    Finally if it's only a narrow range of components in your output vector, check for precision errors and make sure MATLAB is set to a high precision.

    Other than that, you've got me!

    Good luck!
     
  14. Jul 18, 2012 #13
    Thanks for the help, I'll keep working on it
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Calculate amplitude, phase and energy spectrum using FFT in MATLAB
Loading...