一维的opencv dft与matlab fft

Posted

技术标签:

【中文标题】一维的opencv dft与matlab fft【英文标题】:opencv dft vs. matlab fft for 1D 【发布时间】:2015-03-24 12:27:42 【问题描述】:

这是我的输入一维数组:

 [13.9284, 14.5602, 15.5242, 16.4924, 17.4642, 18.2483, 19.2354, 20.2237, 21.095, 21.0238, 22, 23, 24.0208, 23.0868, 24.1868, 25.1794, 26.0768, 27.074, 28.0713, 27.074, 26.0768, 25.0799, 24.0208, 25, 26, 27, 28, 27.0185, 28.0713, 29.0689, 30.0167, 30, 30, 30.0167, 31, 32, 33.0151, 34.0147, 33, 32, 31.0161, 32.0624, 33.1361, 34.0588, 35.0571, 36.0139, 36, 37, 38.0132, 37.054, 37.1214, 36.2215, 35.2278, 34.1321, 33.2415, 33.3766, 34.5254, 35.5106, 36.6742, 37.6563, 36.6742, 35.5106, 34.5254, 33.3766, 32.3883, 31.257, 30.1496, 29.2746, 29.4279, 30.5941, 30.8058, 30.0832, 29.1204, 28.1603, 27.2029, 25.9615, 25.2982, 24.0416, 23.0868, 22.1359, 21.1896, 20.2485, 19.3132, 18.3848, 17.088, 16.1555, 15.2315, 14.3178, 13.4164, 12.53, 11.1803, 10.2956, 9.43398, 8.60233, 7.81025, 6.40312, 5.65685, 5, 4.47214, 4.12311, 4, 3, 3.16228, 3.60555, 4.24264, 5, 5.83095, 6.7082, 7.28011, 8.24621, 9.21954, 10.198, 11.0454, 12.0416, 13.0384, 14.0357, 15.0333, 16.0312, 17.0294, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28.0179, 29.0172, 30.0167, 31.0161, 32.0156, 33.0151, 34.0147, 35.0143, 36.0555, 37.054, 38.0526, 39.0512, 40.05, 41.1096, 42.107, 43.1045, 44.1022, 45.0999, 46.0977, 47.0956, 48.1664, 49.163, 50.1597, 51.1566, 52.2398, 53.2353, 54.231, 55.2268, 56.2228, 55.2268, 54.231, 53.2353, 52.2398, 51.2445, 50.2494, 49.2544, 48.2597, 47.2652, 46.2709, 45.2769, 44.2832, 43.2897, 42.2966, 41.3038, 40.3113, 39.3192, 38.3275, 37.3363, 36.3456, 35.3553, 34.3657, 33.3766, 32.3883, 31.4006, 30.4138, 29.4279, 28.4429, 27.4591, 26.4764, 25.4951, 24.5153, 23.5372, 22.561, 21.587, 20.6155, 19.6469, 18.6815, 17.72, 16.7631, 15.8114, 14.8661, 13.9284, 13, 12.083, 11.1803, 10.2956, 9.43398, 8.60233, 7.81025, 7.07107, 6.40312, 5.83095, 5.38516, 5.09902, 5, 5, 5.09902, 5.38516, 5.83095, 6.40312, 7.07107, 7.81025, 8.60233, 9.43398, 10.2956, 11.1803, 12.083, 13]

当我跑步时 Matlab fft 我得到的结果不同于

来自 Matlab 的回答:

5850.44082000000 + 0.00000000000000i    -236.195108992328 + 494.327104308963i   -1601.71373056680 - 1257.19679210623i   401.270621054570 - 54.8348065835625i    58.6362849360901 - 163.991208417496i    -81.1618489916308 - 60.6727729546051i   98.2137509701359 - 178.874979649065i    8.75660326872264 + 31.1059211928010i    -14.4396596622584 - 7.65407579243336i   -9.18663229218099 - 27.1997908482849i   32.5503165463479 + 24.3192745247901i    -31.1219011708661 + 25.7860663846281i   2.95031666949167 + 6.15041271891396i    15.5915878000708 + 7.44141735494909i    6.97702383662843 + 35.5170021796418i    23.6281937003968 - 21.1006242353996i    5.97648257401926 - 39.6138615794895i    -33.0032997489133 + 1.30076978156147i   -15.0418122371750 + 24.6819515735041i   31.1393323965219 - 10.0379855452569i    -8.54814236721161 - 24.2416895900915i   -39.1574989954314 + 6.70015288183043i   -5.60660423183304 + 23.6679501739800i   19.1780240882932 + 15.5704394051741i    1.30717784406095 + 3.68504588500404i    7.45795750406563 + 3.39486331858465i    17.5245792950556 - 4.31294325456059i    2.41873595069340 - 7.44453173664043i    -6.00947311352358 - 12.1313710088946i   -4.18154377130777 - 2.50225243762004i   -2.20651365633251 + 4.72665264429753i   -1.72270341112623 + 0.197689181530038i  3.48935944436198 - 8.66327988659334i    -11.8221389770113 - 2.73492738484021i   -13.4251149477799 + 12.7213800072862i   3.73623581925574 + 12.7809120584468i    13.9007662712349 + 8.74121680636068i    6.24770455302712 - 2.00106833739501i    6.18955793386744 - 1.78052956088457i    9.37882547144118 - 10.4387804557484i    -10.2820486114914 - 7.56460553957821i   -6.91793379409868 + 3.37859410905941i   2.26246595365120 + 2.85055700831106i    2.97164192031584 + 1.19638197424677i    -2.32764734219557 - 3.52146253780298i   -0.128456276053257 + 0.371511723725504i 2.51404210613061 - 0.368321502067278i   -3.36759807322456 - 1.99977119992005i   -0.550861771421822 - 0.781259371882626i 0.654734330528342 - 1.10909753428241i   -5.10226403227105 - 0.649609303524812i  -4.35743957552811 + 2.03780871572405i   0.789562920892216 + 5.02843929868241i   1.31772372300086 + 3.20064305370176i    -0.140064442432518 + 0.715606830659397i 2.55731297616605 + 1.22415928937412i    0.972383057822540 + 1.11452954184361i   -1.09766497930718 + 0.128751181670111i  0.295033690905171 + 0.753971522354846i  1.55444641432210 + 3.35145255172278i    0.374562941292385 + 2.08937979612806i   5.29852099293259 + 1.37829167261507i    5.54859012001244 - 3.21257680351025i    -1.07668822076172 - 3.30955388709968i   -1.37257720079793 - 2.44245043665786i   1.40536606890888 + 1.66440564981678i    2.49435403989193 - 2.58415130217770i    -3.28563738557140 - 4.05477826915994i   -0.518407813348183 + 0.131067263982409i -2.04925759851836 - 0.262283316140675i  -2.41969646059706 + 0.777798355648820i  -0.134139765532215 + 0.907106120270047i 0.730399953450643 + 1.65851073839491i   -1.45678885233597 - 1.16372102129882i   -2.20160324624550 + 1.32325219820590i   0.579604238352069 + 3.38240693896227i   0.831075445733614 + 3.24321494006862i   4.28901429023598 - 0.0874581930640304i  1.49823315525401 - 2.39040184090840i    -0.377513297736293 + 1.16391842543968i  2.06813869338537 - 1.19345092081081i    2.05985213152420 - 2.22716482633267i    -0.442563051798736 - 3.60274864824367i  -5.60740416812049 - 2.10873191433014i   -2.28409332116379 + 3.25751007842877i   0.433042603344951 + 3.05868696142239i   1.02072812804223 + 1.96653338929739i    1.84085146695848 - 0.923985120389667i   0.388629240309292 + 0.838212051807965i  1.17786879322356 + 0.400582810627874i   0.259566861526970 - 1.80532655327674i   0.372659878189257 + 0.0860665525151383i -0.467279393929562 + 0.0194961351504794i    -0.788639270316992 - 0.728110745822363i -0.525425582048058 + 1.05458284528174i  1.09457353336624 + 2.35618849514349i    1.29718434477957 - 0.926336813481122i   0.654663687830375 - 0.243309093614304i  1.46292093798042 + 0.514645032758256i   1.57935027555005 - 0.946259927194781i   0.997945072809900 - 3.95889770752841i   -2.11126597496163 - 1.85525696046438i   -2.85375151259268 - 0.665127010849972i  -3.67649131275672 + 1.49357708053631i   2.02842018202423 + 3.43084304311646i    0.587989734036455 - 0.665601892824327i  -0.885597237730284 - 0.276649300905060i -0.285389790393298 - 0.374323003019116i -1.75323182180390 + 2.12902138070610i   -0.394442022274234 + 1.59875943868965i  0.770938958112609 + 1.84913363734508i   1.83828912016749 + 2.31866799695042i    1.02615442076382 + 0.996282806003860i   3.01830600098366 + 0.528644305273687i   3.01830600098366 - 0.528644305273687i   1.02615442076382 - 0.996282806003860i   1.83828912016749 - 2.31866799695042i    0.770938958112609 - 1.84913363734508i   -0.394442022274234 - 1.59875943868965i  -1.75323182180390 - 2.12902138070610i   -0.285389790393298 + 0.374323003019116i -0.885597237730284 + 0.276649300905060i 0.587989734036455 + 0.665601892824327i  2.02842018202423 - 3.43084304311646i    -3.67649131275672 - 1.49357708053631i   -2.85375151259268 + 0.665127010849972i  -2.11126597496163 + 1.85525696046438i   0.997945072809900 + 3.95889770752841i   1.57935027555005 + 0.946259927194781i   1.46292093798042 - 0.514645032758256i   0.654663687830375 + 0.243309093614304i  1.29718434477957 + 0.926336813481122i   1.09457353336624 - 2.35618849514349i    -0.525425582048058 - 1.05458284528174i  -0.788639270316992 + 0.728110745822363i -0.467279393929562 - 0.0194961351504794i    0.372659878189257 - 0.0860665525151383i 0.259566861526970 + 1.80532655327674i   1.17786879322356 - 0.400582810627874i   0.388629240309292 - 0.838212051807965i  1.84085146695848 + 0.923985120389667i   1.02072812804223 - 1.96653338929739i    0.433042603344951 - 3.05868696142239i   -2.28409332116379 - 3.25751007842877i   -5.60740416812049 + 2.10873191433014i   -0.442563051798736 + 3.60274864824367i  2.05985213152420 + 2.22716482633267i    2.06813869338537 + 1.19345092081081i    -0.377513297736293 - 1.16391842543968i  1.49823315525401 + 2.39040184090840i    4.28901429023598 + 0.0874581930640304i  0.831075445733614 - 3.24321494006862i   0.579604238352069 - 3.38240693896227i   -2.20160324624550 - 1.32325219820590i   -1.45678885233597 + 1.16372102129882i   0.730399953450643 - 1.65851073839491i   -0.134139765532215 - 0.907106120270047i -2.41969646059706 - 0.777798355648820i  -2.04925759851836 + 0.262283316140675i  -0.518407813348183 - 0.131067263982409i -3.28563738557140 + 4.05477826915994i   2.49435403989193 + 2.58415130217770i    1.40536606890888 - 1.66440564981678i    -1.37257720079793 + 2.44245043665786i   -1.07668822076172 + 3.30955388709968i   5.54859012001244 + 3.21257680351025i    5.29852099293259 - 1.37829167261507i    0.374562941292385 - 2.08937979612806i   1.55444641432210 - 3.35145255172278i    0.295033690905171 - 0.753971522354846i  -1.09766497930718 - 0.128751181670111i  0.972383057822540 - 1.11452954184361i   2.55731297616605 - 1.22415928937412i    -0.140064442432518 - 0.715606830659397i 1.31772372300086 - 3.20064305370176i    0.789562920892216 - 5.02843929868241i   -4.35743957552811 - 2.03780871572405i   -5.10226403227105 + 0.649609303524812i  0.654734330528342 + 1.10909753428241i   -0.550861771421822 + 0.781259371882626i -3.36759807322456 + 1.99977119992005i   2.51404210613061 + 0.368321502067278i   -0.128456276053257 - 0.371511723725504i -2.32764734219557 + 3.52146253780298i   2.97164192031584 - 1.19638197424677i    2.26246595365120 - 2.85055700831106i    -6.91793379409868 - 3.37859410905941i   -10.2820486114914 + 7.56460553957821i   9.37882547144118 + 10.4387804557484i    6.18955793386744 + 1.78052956088457i    6.24770455302712 + 2.00106833739501i    13.9007662712349 - 8.74121680636068i    3.73623581925574 - 12.7809120584468i    -13.4251149477799 - 12.7213800072862i   -11.8221389770113 + 2.73492738484021i   3.48935944436198 + 8.66327988659334i    -1.72270341112623 - 0.197689181530038i  -2.20651365633251 - 4.72665264429753i   -4.18154377130777 + 2.50225243762004i   -6.00947311352358 + 12.1313710088946i   2.41873595069340 + 7.44453173664043i    17.5245792950556 + 4.31294325456059i    7.45795750406563 - 3.39486331858465i    1.30717784406095 - 3.68504588500404i    19.1780240882932 - 15.5704394051741i    -5.60660423183304 - 23.6679501739800i   -39.1574989954314 - 6.70015288183043i   -8.54814236721161 + 24.2416895900915i   31.1393323965219 + 10.0379855452569i    -15.0418122371750 - 24.6819515735041i   -33.0032997489133 - 1.30076978156147i   5.97648257401926 + 39.6138615794895i    23.6281937003968 + 21.1006242353996i    6.97702383662843 - 35.5170021796418i    15.5915878000708 - 7.44141735494909i    2.95031666949167 - 6.15041271891396i    -31.1219011708661 - 25.7860663846281i   32.5503165463479 - 24.3192745247901i    -9.18663229218099 + 27.1997908482849i   -14.4396596622584 + 7.65407579243336i   8.75660326872264 - 31.1059211928010i    98.2137509701359 + 178.874979649065i    -81.1618489916308 + 60.6727729546051i   58.6362849360901 + 163.991208417496i    401.270621054570 + 54.8348065835625i    -1601.71373056680 + 1257.19679210623i   -236.195108992328 - 494.327104308963i

openCV 2.4.10:

cv::vector<float> distanceF; // this is the type of the vector
cv::dft(distanceF, ff, cv::DFT_ROWS|cv::DFT_COMPLEX_OUTPUT);

OpenCV 输出请注意有些值是一样的:

[5850.4424, 0, -236.19542, 494.32721, -1601.7139, -1257.1968, 401.27112, -54.834793, 58.635941, -163.99118, -81.161819, -60.672802, 98.213882, -178.8752, 8.7567081, 31.105858, -14.439482, -7.6539149, -9.1864853, -27.199965, 32.550457, 24.319056, -31.122013, 25.786121, 2.9505329, 6.1505966, 15.59177, 7.4411783, 6.9766369, 35.51675, 23.628296, -21.100199, 5.9758949, -39.613434, -33.00349, 1.3006741, -15.041353, 24.681818, 31.139578, -10.038104, -8.5481758, -24.24147, -39.157513, 6.7004414, -5.6067162, 23.668552, 19.177927, 15.570613, 1.3069649, 3.6849215, 7.4576416, 3.3949444, 17.524834, -4.3131351, 2.4190979, -7.4447069, -6.009819, -12.131269, -4.1815758, -2.5021544, -2.2066612, 4.7267613, -1.722599, 0.1980657, 3.4889717, -8.66294, -11.822751, -2.7352583, -13.425085, 12.721531, 3.7364502, 12.780836, 13.900464, 8.7409039, 6.2476082, -2.000788, 6.1896486, -1.7802718, 9.3790874, -10.438701, -10.282202, -7.5648155, -6.9182959, 3.3786888, 2.2623367, 2.8504691, 2.9716797, 1.1957676, -2.3280506, -3.521215, -0.12844467, 0.37162167, 2.513998, -0.36863309, -3.3680859, -1.9997617, -0.55117607, -0.78108609, 0.6547966, -1.1083429, -5.1021919, -0.64962733, -4.3571014, 2.0373371, 0.78962708, 5.0286837, 1.3172665, 3.2008133, -0.14001274, 0.71585965, 2.5574074, 1.2240793, 0.97222137, 1.1142333, -1.0971603, 0.12921375, 0.29561615, 0.75393069, 1.5542717, 3.3511853, 0.37465286, 2.0893302, 5.2984409, 1.3784981, 5.548686, -3.2126107, -1.076519, -3.3098588, -1.3726921, -2.4427652, 1.4058294, 1.6643414, 2.4948006, -2.5840356, -3.2862215, -4.0548897, -0.51816654, 0.13079435, -2.0494299, -0.26226592, -2.4197426, 0.77750373, -0.13409233, 0.90709144, 0.73083496, 1.6584089, -1.4566784, -1.163747, -2.2017193, 1.3230393, 0.57952499, 3.3821714, 0.83114338, 3.2429562, 4.2885866, -0.087527037, 1.4980612, -2.3904862, -0.37744904, 1.1637971, 2.0684986, -1.1937973, 2.0592871, -2.227041, -0.44268799, -3.6024194, -5.6071653, -2.1089995, -2.2843628, 3.2571955, 0.43229103, 3.0588517, 1.0210943, 1.9662709, 1.8409662, -0.9237324, 0.3887701, 0.83835888, 1.1778173, 0.40068072, 0.2591362, -1.805531, 0.37266827, 0.085778832, -0.46706057, 0.019422591, -0.78862524, -0.72800255, -0.52543736, 1.0548751, 1.0942721, 2.3556924, 1.2972126, -0.92645401, 0.65454626, -0.24292475, 1.4631515, 0.51448888, 1.5789657, -0.94608289, 0.99792957, -3.9589138, -2.1106853, -1.8556111, -2.8534913, -0.66516978, -3.6764612, 1.493674, 2.0280688, 3.4307818, 0.58782125, -0.66583544, -0.88579464, -0.27679062, -0.28537679, -0.37455428, -1.7532051, 2.1291695, -0.39484131, 1.598603, 0.77090323, 1.8497298, 1.8382895, 2.3185685, 1.0264703, 0.99646896, 3.0183427, 0.52932495, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]

能否请您告诉我是否有办法获得与 matlab 相同的结果? 这个数字 -4.3160208e+008, -4.3160208e+008 一遍又一遍地出现。这是转换问题吗?

【问题讨论】:

你能展示不同的结果吗? @Kornel 我添加了标志 DFT_ROWS 并添加了结果 【参考方案1】:

以供将来参考,此测试与 matlab 的 1D 结果有点准确

    cv::Mat ff;
    cv::dft(distanceF, ff, cv::DFT_ROWS|cv::DFT_COMPLEX_OUTPUT);

    //Make place for both the complex and the real values
    cv::Mat planes[] = cv::Mat::zeros(distanceF.size(),1, CV_32F), cv::Mat::zeros(distanceF.size(),1, CV_32F);
    cv::split(ff, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))

    int m = planes[0].cols;
    int pivot = ceil(m/2);
    //duplicate FFT results with Complex conjugate in order to get exact matlab results

    for (int i = pivot + 1, k = pivot; i < planes[1].cols; i++, k--)
    
         planes[1].at<float>(i) = planes[1].at<float>(k) * -1; 
         planes[0].at<float>(i) = planes[0].at<float>(k);
       

【讨论】:

【参考方案2】:

您正在获取真实数据的fft,因此傅立叶咖啡具有 Hermite 对称性c(k)=conjugate(c(-k)),其中k 是系数的索引,或者如果想说的话是波数。

Matlab 是这样排列输出的

[c(0),c(1),c(2),...,c(N/2),c(-N/2),c(-N/2+1),...,c(-1)]

换句话说,它将频谱的负部分放在正部分旁边。

现在,由于上述真实数据傅里叶变换的对称性,表示傅里叶谱的两半是多余的(一个只是另一个的复共轭)。 Matlab 无论如何都会这样做,但 OpenCV 似乎并非如此。如果您注意到,奇怪的数字 -4.3160208e+008 出现在 OpenCV 输出的后半部分,在我看来这只是某种虚拟填充。 基本上,你应该忽略输出的后半部分,真正的输出只是前半部分。

【讨论】:

@caludv 所以你是说,matlab 返回 227 个复数,我需要忽略其中的一半?并且 open cv 返回 454 个数字,它们是 227 个复数(1 表示实数,1 表示虚数),我也应该忽略其中的一半)? Matlab 输出都很好,有冗余,因为它包含频谱的两半。由您决定在以下计算中是否需要同时使用前半部分或仅使用前半部分(以提高效率)。使用打开的简历,您必须删除下半部分(所有-4.3160208e + 008),因为它只是垃圾。是的,open cv 将结果存储在 2*N 实数数组(real,imag,real,imag,...)中,而 Matlab 使用 N 复数数组。 @caludv 如何从 openCV DFT 中查看输出矩阵的一个值? 我如何从 ff 矩阵中获取 cv::mat ,我不知道它是 DFT 后的类型,例如位置 0 处的值?我通常使用类似 ff.at(0);但它在 DFT 之后不起作用 @Caludev 所以openCV只恢复了一半的频谱?我可以让它返回两半吗?

以上是关于一维的opencv dft与matlab fft的主要内容,如果未能解决你的问题,请参考以下文章

记录一个问题opencv中 cv::dft()与cv::ocl_dft()计算的结果相差较大

C++,OpenCV:异步运行 cuda::dft

使用 OpenCV 保存 DFT 的频谱

源码阅读opencv中opencl版本的dft函数的实现细节

《DSP using MATLAB》示例Example5.6

数字水印基于matlab DFT数字水印嵌入提取攻击含Matlab源码 2320期