奇怪的 C 程序延迟行为
Strange C program Latency behaviour
正在编写一个简单的 (vanila) DFT 程序并观察到异常行为:
重新创建
编译以下程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate 2*pi*50*t sine wave in range (-1,+1)
float y[] = {
0.0, 0.0984079408900634, 0.195860568724094, 0.291411844321996, 0.384134186227703, 0.473127475375545, 0.557527793160445, 0.636515808085535, 0.709324729572274, 0.775247751720011, 0.833644914753334, 0.883949317548447, 0.925672620929349, 0.958409788309562, 0.981843017658750, 0.995744826623968,
0.999980260856137, 0.994508204104035, 0.979381777357810, 0.954747823167273, 0.920845480141026, 0.878003861464755, 0.826638859974879, 0.767249110802900, 0.700411150783807, 0.626773821619470, 0.547051971129342, 0.462019513734716, 0.372501917543151, 0.279368190966096, 0.183522446661037, 0.0858951246927854,
-0.0125660398833526, -0.110905217332694, -0.208167762132862, -0.303409480369524, -0.395705795671223, -0.484160724703344, -0.567915575089941, -0.646157281326779, -0.718126297763189, -0.783123972030169, -0.840519327335832, -0.889755187787833, -0.930353587280090, -0.961920409436037, -0.984149213565316, -0.996824209492737,
-0.999822352380809, -0.993114537209969, -0.976765881320872, -0.950935092275919, -0.915872927176609, -0.871919758393194, -0.819502269337733, -0.759129312356960, -0.691386968955206, -0.616932860301177, -0.536489763250371, -0.450838593856768, -0.360810826487642, -0.267280422134304, -0.171155344276191, -0.0733687446595907,
0.0251300954433374, 0.123384980555854, 0.220442083438833, 0.315359204542867, 0.407214918582441, 0.495117519440145, 0.578213676566644, 0.655696718842807, 0.726814465486903, 0.790876527986881, 0.847261012172933, 0.895420555368697, 0.934887640014398, 0.965279132178928, 0.986300000902440, 0.997746182263274,
0.999506560365732, 0.991564046017929, 0.973995742628244, 0.946972197709905, 0.910755747259856, 0.865697969084168, 0.812236269792286, 0.750889638592492, 0.682253609109396, 0.606994478132532, 0.525842837418596, 0.439586481338564, 0.349062759219976, 0.255150446625443, 0.158761214478388, 0.0608307788510959,
-0.0376901826699336, -0.135845259860259, -0.232681594384382, -0.327259129842054, -0.418659737537428, -0.505996129381040, -0.588420471400950, -0.665132614245854, -0.735387860781017, -0.798504195371694, -0.853868904674451, -0.900944525663825, -0.939274063152787, -0.968485426156976, -0.988295040035869, -0.998510599345391,
-0.999032934678125, -0.989856975368447, -0.971071798716938, -0.942859765256222, -0.905494748452794, -0.859339476030335, -0.804842008724098, -0.742531390649502, -0.673012513509774, -0.596960244499286, -0.515112874907027, -0.428264953019107, -0.337259570896168, -0.242980179903264, -0.146342014444403, -0.0482832071574495,
0.0502443181797695, 0.148284087623366, 0.244884362208849, 0.339107377130930, 0.430038445266602, 0.516794836667414, 0.598534347821637, 0.674463477498649, 0.743845129807025, 0.806005769687504, 0.860341961377560, 0.906326226374270, 0.943512164028194, 0.971538785059195, 0.990134015925798, 0.999117340028769,
0.998401550108975, 0.987993594827769, 0.967994511311594, 0.938598444315629, 0.900090761528124, 0.852845283311457, 0.797320653772707, 0.734055888392230, 0.663665141432458, 0.586831743923250, 0.504301570101134, 0.416875796698963, 0.325403125376477, 0.230771543793960, 0.133899705309884, 0.0357280109856745,
-0.0627905195293142, -0.160699499610071, -0.257048459953733, -0.350902075433899, -0.441349244940104, -0.527511936058200, -0.608553708730373, -0.683687835149718, -0.752184937064112, -0.813380066348762, -0.866679160111565, -0.911564807667102, -0.947601273395360, -0.974438726724819, -0.991816638176614, -0.999566308502021,
-0.997612506361225, -0.985974198645282, -0.964764366351565, -0.934188907800099, -0.894544639838025, -0.846216416435848, -0.789673392647252, -0.725464470200735, -0.654212968935859, -0.576610575812079, -0.493410630231271, -0.405420810857945, -0.313495294931096, -0.218526466182705, -0.121436251859683, -0.0231671729467908,
0.0753268055279309, 0.173089535282900, 0.269171966766988, 0.362641362231371, 0.452590350451546, 0.538145735199171, 0.618476971953920, 0.692804230566056, 0.760405965600031, 0.820625920868545, 0.872879500159325, 0.916659442309612, 0.951540745536515, 0.977184793219440, 0.993342641082758, 0.999857433867821,
0.996665928034030, 0.983799105707054, 0.961381873914185, 0.929631852026567, 0.888857259179404, 0.839453922178432, 0.781901432938911, 0.716758492759546, 0.644657488627590, 0.566298354206707, 0.482441775103100, 0.393901804371106, 0.301537959944494, 0.206246880709201, 0.108953622217581, 0.0106026765427436,
-0.0878511965507475, -0.185452238111593, -0.281252968206354, -0.374323383753877, -0.463759986700061, -0.548694554890174, -0.628302570493986, -0.701811224163148, -0.768506917219076, -0.827742189042424, -0.878942002415270, -0.921609325799925, -0.955329958363339, -0.979776550907327, -0.994711783670682, -0.999990670154097,
-0.995561964603080, -0.981468659485473, -0.957847568134224, -0.924927996606965, -0.883029517655600, -0.832558868415444, -0.774006001930223, -0.707939330843420, -0.635000209428760, -0.555896707526464, -0.471396736825997, -0.382320596223098, -0.289533008618479, -0.193934726462339, -0.0964537875354998, 0.00196349414684702,
0.100361714851215, 0.197785655882052, 0.293289556541649, 0.385946295274789, 0.474856389870597, 0.559156729350287, 0.638028952774654, 0.710707393632289, 0.776486512687080, 0.834727747129162, 0.884865709540016, 0.926413676494061, 0.958968313515203, 0.982213590519895, 0.995923849736900, 0.999965996321297,
0.994300790396999, 0.978983227985015, 0.954162007119530, 0.920078084334595, 0.877062335534554, 0.825532343955807, 0.765988346401266, 0.699008377100263, 0.625242656335702, 0.545407278311946, 0.460277259539545, 0.370679015220949, 0.277482336674029, 0.181591947674010, 0.0839387216822394, -0.0145293547779467,
-0.112856384873481, -0.210087841004649, -0.305279831056042, -0.397508261401654, -0.485877807712459, -0.569530606480889, -0.647654582887413, -0.719491334165196, -0.784343491933411, -0.841581492028159, -0.890649686111535, -0.931071735729352, -0.962455236453648, -0.984495527220340, -0.996978647882129, -0.999783416265704,
-0.992882604569814, -0.976343203684123, -0.950325772862918, -0.915082881066822, -0.870956655103499, -0.818375458369187, -0.757849732432795, -0.689967041831200, -0.615386370179169, -0.534831722965621, -0.449085099138105, -0.358978899705249, -0.265387847051938, -0.169220493412062, -0.0714104009317734, 0.0270929210554974,
0.125333233564306, 0.222356850821733, 0.317221898346202, 0.409007456365973, 0.496822499816019, 0.579814548126516, 0.657177940833683, 0.728161658675826, 0.792076614249970, 0.848302341453640, 0.896293018772879, 0.935582767944242, 0.965790176553122, 0.986622000664402, 0.997876011541515, 0.999442958818825,
0.991307631069506, 0.973549003473241, 0.946339471150244, 0.909943175604153, 0.864713440520154, 0.811089341810776, 0.749591445206302, 0.680816752767877, 0.605432907381000, 0.524171711490274, 0.437822022993559, 0.347222097259867, 0.253251449612327, 0.156822317272532, 0.0588708036511916, -0.0396522090467458,
-0.137790290684639, -0.234590747914437, -0.329113872621261, -0.420442064311573, -0.507688737887499, -0.590006930333573, -0.666597522764850, -0.736716998019437, -0.799684658487090, -0.854889234105571, -0.901794816376402, -0.939946060794457, -0.968972607187923, -0.988592675057273, -0.998615799010932, -0.998944677742839,
-0.989576118602650, -0.970601068588971, -0.942203731464759, -0.904659779565662, -0.858333677660472, -0.803675144842848, -0.741214788801068, -0.671558954847019, -0.595383839708351, -0.513428927225288, -0.426489809676181, -0.335410464420208, -0.241075060833035, -0.144399377071172, -0.0463219099882471, 0.0522052354945227,
0.150225589120753, 0.246787600408572, 0.340953876000616, 0.431810279581299, 0.518474806021958, 0.600106143606748, 0.675911841219724, 0.745156001208767, 0.807166423246402, 0.861341129837251, 0.907154210124478, 0.944160925265473, 0.972002025815363, 0.990407239206624, 0.999197893469363, 0.998288651722103,
0.987688340595138, 0.967499864544408, 0.937919206887704, 0.899233527260826, 0.851818373962972, 0.796134038253055, 0.732721085988247, 0.662195109982227, 0.585240754025510, 0.502605066580843, 0.415090248673835, 0.323545866380009, 0.228860603507013, 0.131953634534243, 0.0337657015587562, -0.0647500181304328,
-0.162637165194890, -0.258945482279751, -0.352740038810489, -0.443110307002175, -0.529179000974195, -0.610110593163201, -0.685119425359456, -0.753477335627384, -0.814520727070512, -0.867657009819546, -0.912370353706714, -0.948226695781328, -0.974877954055122, -0.992065406571739, -0.999622202997347, -0.997474984350731,
-0.985644595148997, -0.964245881055618, -0.933486573995179, -0.893665275557789, -0.845168558269628, -0.788467212869540, -0.724111678021981, -0.652726696833165, -0.575005252043276, -0.491701838769995, -0.403625140109338, -0.311630176696852, -0.216610006438655, -0.119487054988758, -0.0212041611335505, 0.0772845759878599,
0.175023058975285, 0.271062473657499, 0.364470499879154, 0.454340362168866, 0.539799632427741, 0.620018699184368, 0.694218821199767, 0.761679687240114, 0.821746408629595, 0.873835876701791, 0.917442423433567, 0.952142730309724, 0.977599937764792, 0.993566915308769, 0.999888660591489, 0.996503804116233,
0.983445204995330, 0.960839631964316, 0.928906532751298, 0.887955903748049, 0.838385280663432, 0.780675879372910, 0.715387924427582, 0.643155210572020, 0.564678950066079, 0.480720965538817, 0.392096294456247, 0.299665276996257, 0.204325204139264, 0.107001607052153, 0.00863927232549537, -0.0898069297148414,
-0.187381314585722, -0.283136661128381, -0.376143406830793, -0.465498671725509, -0.550335023261759, -0.629828897065438, -0.703208591840560, -0.769761760800563, -0.828842326904764, -0.879876754769274, -0.922369618366427, -0.955908410463418, -0.980167547111590, -0.994911528312078, -0.999997224175046, -0.995375264379227,
-0.981090517443334, -0.957281655156723, -0.924179806397673, -0.882106313407595, -0.831469612302545, -0.772761268105050, -0.706551202786880, -0.633482162647440, -0.554263478736693, -0.469664180894493, -0.380505532252943, -0.287653056674601, -0.192008136521520, -0.0944992623213991, 0.00392698072380246, 0.102315101886539,
0.199709980514407, 0.295166138038260, 0.387756916378113, 0.476583473645635, 0.560783509815936, 0.639539637662464, 0.712087317692860, 0.777722280055610, 0.835807361368273, 0.885778690097328, 0.927151160444101, 0.959523141597856, 0.982580376640236, 0.996099033251688, 0.999947876604571, 0.994089543349219,
0.978580904325473, 0.953572512478617, 0.919307141339182, 0.876117428254551, 0.824422645251177, 0.764724628874819, 0.697602908520652, 0.623709080545813, 0.543760482778796, 0.458533230831479, 0.368854683815130, 0.275595412600709, 0.179660748593197, 0.0819819950616476, -0.0164926136573281, -0.114807117317581,
-0.212007109922050, -0.307149004793269, -0.399309194613317, -0.487593017510555, -0.571143442153087, -0.649149387536929, -0.720853596702917, -0.785559987947003, -0.842640412160429, -0.891540750701937, -0.931786294605665, -0.962986352905092, -0.984838045336978, -0.997129242606799, -0.999740625672616, -0.992646844056464,
-0.975916761938747, -0.949712789646630, -0.914289307026130, -0.869990194003309, -0.817245492307110, -0.756567230760720, -0.688544454668300, -0.613837507550104, -0.533171620737190, -0.447329873055838, -0.357145588946850, -0.263494248816364, -0.167284990149970, -0.0694517818945103, 0.0290556422159688, 0.127281003373913,
0.224270760949379, 0.319083369159829, 0.410798417297745, 0.498525564785646, 0.581413184319829, 0.658656629197908, 0.729506044573661, 0.793273646809804, 0.849340400263315, 0.897162026686948, 0.936274288909695, 0.966297497503909, 0.986940196689757, 0.998001993695415, 0.999375504106507, 0.991047394319906,
0.973098510982126, 0.945703096155735, 0.909127095832718, 0.863725578215192, 0.809939286825997, 0.748290361910482, 0.679377271664694, 0.603869002496122, 0.522498564715948, 0.436055876707622, 0.345380096649905, 0.251351476235615, 0.154882815467577, 0.0569106014859085, -0.0416140825518728, -0.139734790284324,
-0.236498997023725, -0.330967346563474, -0.422222770149962, -0.509379389094972, -0.591591114604805, -0.668059861341758, -0.738043294983249, -0.800862038567848, -0.855906267671136, -0.902641630387714, -0.940614434649876, -0.969456052526198, -0.988886498744503, -0.998717148700020, -0.998852569563171, -0.989291446711198,
-0.970126596490105, -0.941544065183020, -0.903821322931938, -0.857324570145646, -0.802505182542385, -0.739895329337642, -0.670102807114307, -0.593805139526380, -0.511743000114341, -0.424713022081554, -0.333560064831927, -0.239169012343003, -0.142456182993156, -0.0443604342336034, 0.0541659515417433, 0.152166511451484,
0.248689887164858, 0.342799060386402, 0.433580449132212, 0.520152776493846, 0.601675625794849, 0.677357599089101, 0.746463999800899, 0.808323964926108, 0.862336977557309, 0.907978696511290, 0.944806046466880, 0.972461519199504, 0.990676644157565, 0.999274594689349, 0.998171904620035, 0.987379278514827,
0.967001487762437, 0.937236353487686, 0.898372826166838, 0.850788180588052, 0.794944353387510, 0.731383458715156, 0.660722525562598, 0.583647507841499, 0.500906625360716, 0.413303100345869, 0.321687360012960, 0.226948780890808, 0.130007055036036, 0.0318032619544833, -0.0667092670999465, -0.164574203762531,
-0.260841506289903, -0.354576642263842, -0.444869660735319, -0.530844025739510, -0.611665125428735, -0.686548374219374, -0.754766829299691, -0.815658247559921, -0.868631514438194, -0.913172382273058, -0.948848462456602, -0.975313422925793, -0.992310350244224, -0.999674243636218, -0.997333616761983, -0.985311191684313,
-0.963723678290009, -0.932780641307352, -0.892782465918221, -0.844117441714098, -0.787257993303952, -0.722756094166077, -0.651237908264765, -0.573397711449273, -0.489991151644234, -0.401827913259485, -0.309763857030723, -0.214692711595228, -0.117537397457843, -0.0192410675716325, 0.0792420484915564, 0.176955907898970,
0.272951935517313, 0.366298232379078, 0.456088622261896, 0.541451448559776, 0.621558036048674, 0.695630735402642, 0.762950472366612, 0.822863728301042, 0.874788884333453, 0.918221867529835, 0.952741044274703, 0.978011313356413, 0.993787359023354, 0.999916032431425, 0.996337838364386, 0.983087512794541,
0.960293685676943, 0.928177632250618, 0.887051124968773, 0.837313406910770, 0.779447316057058, 0.714014598051266, 0.641650452951816, 0.563057368911361, 0.478998302644763, 0.390289272887461, 0.297791438744591, 0.202402739831719, 0.105049179362040, 0.00667583480114843, -0.0917623166451206, -0.189309668646243,
-0.285019262469973, -0.377961979757159, -0.467235562107885, -0.551973369919598, -0.631352795449380, -0.704603248428773, -0.771013636709533, -0.829939269320517, -0.880808114923002, -0.923126354909364, -0.956483177237392, -0.980554764463184, -0.995107437258145, -0.999999922893717, -0.995184726672197, -0.980708592990188,
-0.956712051558832, -0.923428053185976, -0.881179708363663, -0.830377150614053, -0.771513555043328, -0.705160350754312, -0.631961673594109, -0.552628113087742, -0.467929814260579, -0.378689001314870, -0.285771995738146, -0.190080806329236, -0.0925443727830621, 0.00589045216103947, 0.104268094465144, 0.201633535202275,
0.297041581577028, 0.389566042557161, 0.478308720042225, 0.562408128285632, 0.641047856924812, 0.713464496433956, 0.778955049061354, 0.836883753308413, 0.886688255700559, 0.927885069936238, 0.960074270418487, 0.982943374605696, 0.996270376492941, 0.999925901775817, 0.993874463775118, 0.978174807930265,
0.952979341517217, 0.918532654127013, 0.875169143267650, 0.823309768139218, 0.763457963095587, 0.696194750463496, 0.622173100162199, 0.542111590878786, 0.456787434334296, 0.367028930359057, 0.273707426020814, 0.177728856863960, 0.0800249523748130, -0.0184558089525184, -0.116757407144309, -0.213925561485702,
-0.309016994374950, -0.401108588363064, -0.489306347484964, -0.572754075888567, -0.650641689512372, -0.722213080124420, -0.786773455380986, -0.843696083650190, -0.892428378123718, -0.932497261154178, -0.963513756742748, -0.985176766594716, -0.997275993086157, -0.999693980766518, -0.992407256578852, -0.975486557728811,
-0.949096144990295, -0.913492208114000, -0.869020378818633, -0.816112375507883, -0.755281812285189, -0.687119212951036, -0.612286278385335, -0.531509462965285, -0.445572922376897, -0.355310901280419, -0.261599634727995, -0.165348841951888, -0.0674928950989053, 0.0310182513578315, 0.129228282475433, 0.226183806443067,
0.320943609807221, 0.412587794473066, 0.500226707783204, 0.583009578983345, 0.660132778234681, 0.730847617997353, 0.794467621051437, 0.850375184599931, 0.898027575760620, 0.936962200244736, 0.966801093075418, 0.987254587751762, 0.998124128239275, 0.999304196488835, 0.990783336772423, 0.972644266891692,
0.945063075179802, 0.908307511091783, 0.862734385977789, 0.808786109271773, 0.746986393721116, 0.677935171349503, 0.602302769507245, 0.520823403546116, 0.434288049289797, 0.343536764491561, 0.249450533820281, 0.152942716540872, 0.0549501799124448, -0.0435757956216944, -0.141678751162651, -0.238406334355361,
-0.332819544522994, -0.424001848187439, -0.511068076485486, -0.593173018107150, -0.669519624338816, -0.739366746559169, -0.802036331074781, -0.856920001450147, -0.903484964433038, -0.941279182142260, -0.969935760307973, -0.989176509964781, -0.998814648021921, -0.998756610494226, -0.989002960791592, -0.969648384249581,
-0.940880768954224, -0.902979381784135, -0.856312157376272, -0.801332126333271, -0.738573017346144, -0.668644075925549, -0.592224150039745, -0.510055100073975, -0.422934597085312, -0.331708379265198, -0.237262041781567, -0.140512439701961, -0.0423987874555876, 0.0561264587622677, 0.154106847132682, 0.250591215143780,
0.344642923174507, 0.435348947094810, 0.521828741614001, 0.603242788335117, 0.678800745532949, 0.747769120540683, 0.809478390263939, 0.863329500698409, 0.908799682356035, 0.945447525145274, 0.972917263440128, 0.990942229739981, 0.999347443393019, 0.998051309252864, 0.987066409778365, 0.966499382887074,
0.936549886748192, 0.897508661564435, 0.849754707158412, 0.793751603762675, 0.730043011729913, 0.659247393850829, 0.582052011513663, 0.499206252988763, 0.411514358605109, 0.319827613440464, 0.225036083316014, 0.128059974319935, 0.0298406997386746, -0.0686682588843781, -0.166510607845101, -0.262736524674396,
-0.356411878713249, -0.446627299356684, -0.532507003934973, -0.613217299533782, -0.687974676220444, -0.756053413109658, -0.816792623431512, -0.869602670210485, -0.913970890274060, -0.949466571024084, -0.975745131657964, -0.992551468249736, -0.999722430218001, -0.997188404139995, -0.984973989536599, -0.963197760067995,
-0.932071112458212, -0.891896214322823, -0.843063070821639, -0.786045738612394, -0.721397723859212, -0.649746608970392, -0.571787960227613, -0.488278575449232, -0.400029137237269, -0.307896343128008, -0.212774589044232, -0.115587286783459, -0.0172778998293627, 0.0811992154923688, 0.178888074602276, 0.274840345062014
};
float freq=0;
float ind=0;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
printf("Freq Compute start\n");
/*
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
}
}
printf("Freq Compute over\n");
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
*/
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}
具有以下编译器标志:
gcc -O3 DFT.c -o DFT_try -lm -lrt
此程序在 BeagleBone Black 上 运行 仅需 3-5 us(微秒),在 PC 上甚至更少。和现在一样,
如果您取消注释下面的 "Freq compute" 部分:
for (k=0;k<2000;k++)
{
val = (Xre[k]*Xre[k])+(Xim[k]*Xim[k]);
if(val> maxV){
printf("%f",val);
maxV = val;
ind = k;
a=val;
}
}
使代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate 2*pi*50*t sine wave in range (-1,+1)
float y[] = {
//<Copy data HERE, from above program };
float freq=0;
float ind=0;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
printf("Freq Compute start\n");
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
}
}
printf("Freq Compute over\n");
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}
然后重新编译,如上
这个程序将花费 16198709 uSec 甚至更多的时间到 BeagleBone Black 上的 运行 和大约 0.001 秒如 chqrlie 在评论中所述,在 64 位现代系统上
谁能告诉我为什么会出现这种奇怪的行为?
当你注释代码块时,编译器可以确定你没有使用第一个循环中的任何计算结果并删除所有代码,留下一个简单的输出语句:
maxV = 0;
ind = 0;
freq = 0;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
如果您取消注释循环,ind
是根据第一个循环中计算的复数值 Xre[k]
和 Xim[k]
计算的,因此它也必须计算这些,因此需要更长的时间计算时间,虽然没有你提到的那么长(我的笔记本电脑上是 1s)。
请注意,更积极的优化器会确定可以在编译时执行计算,并生成一个在两种情况下执行速度都一样快的简单程序。为了避免潜在的无限编译时间,这样的编译器会设置超时并在一段时间后放弃,但在我的笔记本电脑上只需要 1 秒就可以 运行 完整代码。
你的代码问题较多:
您应该为 2*PI 使用更精确的值:2 * M_PI.
y
数组中只有 960 个值。 x
数组中的条目一直到 16383 都是零。可以简化计算:内部循环可以在 y
.
的大小处停止
你在第一个循环中计算了 P[k]
但你没有在第二个循环中使用它。
如果将 N
的值减小到 y
的大小,计算速度会快得多。它 运行 比我的系统快 20 倍:
maxV=228694.692537 ind = 257.000000 Freq= 50.115000
2115850.697709000 47955 uSec
考虑到 Beaglebone 系统与 64 位现代 PC 之间的巨大差异,嵌入式处理器似乎没有浮点单元,使得计算时间 很多 更慢,甚至 float
。请注意,在 PC 上,由于转换次数较少,double
的计算实际上比 float
快得多。
这是更新后的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float a = 1.09; // unused: b = 2.098;
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate random discrete-time signal x in range (-1,+1)
float y[] = {
0.0, 0.0984079408900634, 0.195860568724094, 0.291411844321996, 0.384134186227703, 0.473127475375545, 0.557527793160445, 0.636515808085535, 0.709324729572274, 0.775247751720011, 0.833644914753334, 0.883949317548447, 0.925672620929349, 0.958409788309562, 0.981843017658750, 0.995744826623968,
0.999980260856137, 0.994508204104035, 0.979381777357810, 0.954747823167273, 0.920845480141026, 0.878003861464755, 0.826638859974879, 0.767249110802900, 0.700411150783807, 0.626773821619470, 0.547051971129342, 0.462019513734716, 0.372501917543151, 0.279368190966096, 0.183522446661037, 0.0858951246927854,
-0.0125660398833526, -0.110905217332694, -0.208167762132862, -0.303409480369524, -0.395705795671223, -0.484160724703344, -0.567915575089941, -0.646157281326779, -0.718126297763189, -0.783123972030169, -0.840519327335832, -0.889755187787833, -0.930353587280090, -0.961920409436037, -0.984149213565316, -0.996824209492737,
-0.999822352380809, -0.993114537209969, -0.976765881320872, -0.950935092275919, -0.915872927176609, -0.871919758393194, -0.819502269337733, -0.759129312356960, -0.691386968955206, -0.616932860301177, -0.536489763250371, -0.450838593856768, -0.360810826487642, -0.267280422134304, -0.171155344276191, -0.0733687446595907,
0.0251300954433374, 0.123384980555854, 0.220442083438833, 0.315359204542867, 0.407214918582441, 0.495117519440145, 0.578213676566644, 0.655696718842807, 0.726814465486903, 0.790876527986881, 0.847261012172933, 0.895420555368697, 0.934887640014398, 0.965279132178928, 0.986300000902440, 0.997746182263274,
0.999506560365732, 0.991564046017929, 0.973995742628244, 0.946972197709905, 0.910755747259856, 0.865697969084168, 0.812236269792286, 0.750889638592492, 0.682253609109396, 0.606994478132532, 0.525842837418596, 0.439586481338564, 0.349062759219976, 0.255150446625443, 0.158761214478388, 0.0608307788510959,
-0.0376901826699336, -0.135845259860259, -0.232681594384382, -0.327259129842054, -0.418659737537428, -0.505996129381040, -0.588420471400950, -0.665132614245854, -0.735387860781017, -0.798504195371694, -0.853868904674451, -0.900944525663825, -0.939274063152787, -0.968485426156976, -0.988295040035869, -0.998510599345391,
-0.999032934678125, -0.989856975368447, -0.971071798716938, -0.942859765256222, -0.905494748452794, -0.859339476030335, -0.804842008724098, -0.742531390649502, -0.673012513509774, -0.596960244499286, -0.515112874907027, -0.428264953019107, -0.337259570896168, -0.242980179903264, -0.146342014444403, -0.0482832071574495,
0.0502443181797695, 0.148284087623366, 0.244884362208849, 0.339107377130930, 0.430038445266602, 0.516794836667414, 0.598534347821637, 0.674463477498649, 0.743845129807025, 0.806005769687504, 0.860341961377560, 0.906326226374270, 0.943512164028194, 0.971538785059195, 0.990134015925798, 0.999117340028769,
0.998401550108975, 0.987993594827769, 0.967994511311594, 0.938598444315629, 0.900090761528124, 0.852845283311457, 0.797320653772707, 0.734055888392230, 0.663665141432458, 0.586831743923250, 0.504301570101134, 0.416875796698963, 0.325403125376477, 0.230771543793960, 0.133899705309884, 0.0357280109856745,
-0.0627905195293142, -0.160699499610071, -0.257048459953733, -0.350902075433899, -0.441349244940104, -0.527511936058200, -0.608553708730373, -0.683687835149718, -0.752184937064112, -0.813380066348762, -0.866679160111565, -0.911564807667102, -0.947601273395360, -0.974438726724819, -0.991816638176614, -0.999566308502021,
-0.997612506361225, -0.985974198645282, -0.964764366351565, -0.934188907800099, -0.894544639838025, -0.846216416435848, -0.789673392647252, -0.725464470200735, -0.654212968935859, -0.576610575812079, -0.493410630231271, -0.405420810857945, -0.313495294931096, -0.218526466182705, -0.121436251859683, -0.0231671729467908,
0.0753268055279309, 0.173089535282900, 0.269171966766988, 0.362641362231371, 0.452590350451546, 0.538145735199171, 0.618476971953920, 0.692804230566056, 0.760405965600031, 0.820625920868545, 0.872879500159325, 0.916659442309612, 0.951540745536515, 0.977184793219440, 0.993342641082758, 0.999857433867821,
0.996665928034030, 0.983799105707054, 0.961381873914185, 0.929631852026567, 0.888857259179404, 0.839453922178432, 0.781901432938911, 0.716758492759546, 0.644657488627590, 0.566298354206707, 0.482441775103100, 0.393901804371106, 0.301537959944494, 0.206246880709201, 0.108953622217581, 0.0106026765427436,
-0.0878511965507475, -0.185452238111593, -0.281252968206354, -0.374323383753877, -0.463759986700061, -0.548694554890174, -0.628302570493986, -0.701811224163148, -0.768506917219076, -0.827742189042424, -0.878942002415270, -0.921609325799925, -0.955329958363339, -0.979776550907327, -0.994711783670682, -0.999990670154097,
-0.995561964603080, -0.981468659485473, -0.957847568134224, -0.924927996606965, -0.883029517655600, -0.832558868415444, -0.774006001930223, -0.707939330843420, -0.635000209428760, -0.555896707526464, -0.471396736825997, -0.382320596223098, -0.289533008618479, -0.193934726462339, -0.0964537875354998, 0.00196349414684702,
0.100361714851215, 0.197785655882052, 0.293289556541649, 0.385946295274789, 0.474856389870597, 0.559156729350287, 0.638028952774654, 0.710707393632289, 0.776486512687080, 0.834727747129162, 0.884865709540016, 0.926413676494061, 0.958968313515203, 0.982213590519895, 0.995923849736900, 0.999965996321297,
0.994300790396999, 0.978983227985015, 0.954162007119530, 0.920078084334595, 0.877062335534554, 0.825532343955807, 0.765988346401266, 0.699008377100263, 0.625242656335702, 0.545407278311946, 0.460277259539545, 0.370679015220949, 0.277482336674029, 0.181591947674010, 0.0839387216822394, -0.0145293547779467,
-0.112856384873481, -0.210087841004649, -0.305279831056042, -0.397508261401654, -0.485877807712459, -0.569530606480889, -0.647654582887413, -0.719491334165196, -0.784343491933411, -0.841581492028159, -0.890649686111535, -0.931071735729352, -0.962455236453648, -0.984495527220340, -0.996978647882129, -0.999783416265704,
-0.992882604569814, -0.976343203684123, -0.950325772862918, -0.915082881066822, -0.870956655103499, -0.818375458369187, -0.757849732432795, -0.689967041831200, -0.615386370179169, -0.534831722965621, -0.449085099138105, -0.358978899705249, -0.265387847051938, -0.169220493412062, -0.0714104009317734, 0.0270929210554974,
0.125333233564306, 0.222356850821733, 0.317221898346202, 0.409007456365973, 0.496822499816019, 0.579814548126516, 0.657177940833683, 0.728161658675826, 0.792076614249970, 0.848302341453640, 0.896293018772879, 0.935582767944242, 0.965790176553122, 0.986622000664402, 0.997876011541515, 0.999442958818825,
0.991307631069506, 0.973549003473241, 0.946339471150244, 0.909943175604153, 0.864713440520154, 0.811089341810776, 0.749591445206302, 0.680816752767877, 0.605432907381000, 0.524171711490274, 0.437822022993559, 0.347222097259867, 0.253251449612327, 0.156822317272532, 0.0588708036511916, -0.0396522090467458,
-0.137790290684639, -0.234590747914437, -0.329113872621261, -0.420442064311573, -0.507688737887499, -0.590006930333573, -0.666597522764850, -0.736716998019437, -0.799684658487090, -0.854889234105571, -0.901794816376402, -0.939946060794457, -0.968972607187923, -0.988592675057273, -0.998615799010932, -0.998944677742839,
-0.989576118602650, -0.970601068588971, -0.942203731464759, -0.904659779565662, -0.858333677660472, -0.803675144842848, -0.741214788801068, -0.671558954847019, -0.595383839708351, -0.513428927225288, -0.426489809676181, -0.335410464420208, -0.241075060833035, -0.144399377071172, -0.0463219099882471, 0.0522052354945227,
0.150225589120753, 0.246787600408572, 0.340953876000616, 0.431810279581299, 0.518474806021958, 0.600106143606748, 0.675911841219724, 0.745156001208767, 0.807166423246402, 0.861341129837251, 0.907154210124478, 0.944160925265473, 0.972002025815363, 0.990407239206624, 0.999197893469363, 0.998288651722103,
0.987688340595138, 0.967499864544408, 0.937919206887704, 0.899233527260826, 0.851818373962972, 0.796134038253055, 0.732721085988247, 0.662195109982227, 0.585240754025510, 0.502605066580843, 0.415090248673835, 0.323545866380009, 0.228860603507013, 0.131953634534243, 0.0337657015587562, -0.0647500181304328,
-0.162637165194890, -0.258945482279751, -0.352740038810489, -0.443110307002175, -0.529179000974195, -0.610110593163201, -0.685119425359456, -0.753477335627384, -0.814520727070512, -0.867657009819546, -0.912370353706714, -0.948226695781328, -0.974877954055122, -0.992065406571739, -0.999622202997347, -0.997474984350731,
-0.985644595148997, -0.964245881055618, -0.933486573995179, -0.893665275557789, -0.845168558269628, -0.788467212869540, -0.724111678021981, -0.652726696833165, -0.575005252043276, -0.491701838769995, -0.403625140109338, -0.311630176696852, -0.216610006438655, -0.119487054988758, -0.0212041611335505, 0.0772845759878599,
0.175023058975285, 0.271062473657499, 0.364470499879154, 0.454340362168866, 0.539799632427741, 0.620018699184368, 0.694218821199767, 0.761679687240114, 0.821746408629595, 0.873835876701791, 0.917442423433567, 0.952142730309724, 0.977599937764792, 0.993566915308769, 0.999888660591489, 0.996503804116233,
0.983445204995330, 0.960839631964316, 0.928906532751298, 0.887955903748049, 0.838385280663432, 0.780675879372910, 0.715387924427582, 0.643155210572020, 0.564678950066079, 0.480720965538817, 0.392096294456247, 0.299665276996257, 0.204325204139264, 0.107001607052153, 0.00863927232549537, -0.0898069297148414,
-0.187381314585722, -0.283136661128381, -0.376143406830793, -0.465498671725509, -0.550335023261759, -0.629828897065438, -0.703208591840560, -0.769761760800563, -0.828842326904764, -0.879876754769274, -0.922369618366427, -0.955908410463418, -0.980167547111590, -0.994911528312078, -0.999997224175046, -0.995375264379227,
-0.981090517443334, -0.957281655156723, -0.924179806397673, -0.882106313407595, -0.831469612302545, -0.772761268105050, -0.706551202786880, -0.633482162647440, -0.554263478736693, -0.469664180894493, -0.380505532252943, -0.287653056674601, -0.192008136521520, -0.0944992623213991, 0.00392698072380246, 0.102315101886539,
0.199709980514407, 0.295166138038260, 0.387756916378113, 0.476583473645635, 0.560783509815936, 0.639539637662464, 0.712087317692860, 0.777722280055610, 0.835807361368273, 0.885778690097328, 0.927151160444101, 0.959523141597856, 0.982580376640236, 0.996099033251688, 0.999947876604571, 0.994089543349219,
0.978580904325473, 0.953572512478617, 0.919307141339182, 0.876117428254551, 0.824422645251177, 0.764724628874819, 0.697602908520652, 0.623709080545813, 0.543760482778796, 0.458533230831479, 0.368854683815130, 0.275595412600709, 0.179660748593197, 0.0819819950616476, -0.0164926136573281, -0.114807117317581,
-0.212007109922050, -0.307149004793269, -0.399309194613317, -0.487593017510555, -0.571143442153087, -0.649149387536929, -0.720853596702917, -0.785559987947003, -0.842640412160429, -0.891540750701937, -0.931786294605665, -0.962986352905092, -0.984838045336978, -0.997129242606799, -0.999740625672616, -0.992646844056464,
-0.975916761938747, -0.949712789646630, -0.914289307026130, -0.869990194003309, -0.817245492307110, -0.756567230760720, -0.688544454668300, -0.613837507550104, -0.533171620737190, -0.447329873055838, -0.357145588946850, -0.263494248816364, -0.167284990149970, -0.0694517818945103, 0.0290556422159688, 0.127281003373913,
0.224270760949379, 0.319083369159829, 0.410798417297745, 0.498525564785646, 0.581413184319829, 0.658656629197908, 0.729506044573661, 0.793273646809804, 0.849340400263315, 0.897162026686948, 0.936274288909695, 0.966297497503909, 0.986940196689757, 0.998001993695415, 0.999375504106507, 0.991047394319906,
0.973098510982126, 0.945703096155735, 0.909127095832718, 0.863725578215192, 0.809939286825997, 0.748290361910482, 0.679377271664694, 0.603869002496122, 0.522498564715948, 0.436055876707622, 0.345380096649905, 0.251351476235615, 0.154882815467577, 0.0569106014859085, -0.0416140825518728, -0.139734790284324,
-0.236498997023725, -0.330967346563474, -0.422222770149962, -0.509379389094972, -0.591591114604805, -0.668059861341758, -0.738043294983249, -0.800862038567848, -0.855906267671136, -0.902641630387714, -0.940614434649876, -0.969456052526198, -0.988886498744503, -0.998717148700020, -0.998852569563171, -0.989291446711198,
-0.970126596490105, -0.941544065183020, -0.903821322931938, -0.857324570145646, -0.802505182542385, -0.739895329337642, -0.670102807114307, -0.593805139526380, -0.511743000114341, -0.424713022081554, -0.333560064831927, -0.239169012343003, -0.142456182993156, -0.0443604342336034, 0.0541659515417433, 0.152166511451484,
0.248689887164858, 0.342799060386402, 0.433580449132212, 0.520152776493846, 0.601675625794849, 0.677357599089101, 0.746463999800899, 0.808323964926108, 0.862336977557309, 0.907978696511290, 0.944806046466880, 0.972461519199504, 0.990676644157565, 0.999274594689349, 0.998171904620035, 0.987379278514827,
0.967001487762437, 0.937236353487686, 0.898372826166838, 0.850788180588052, 0.794944353387510, 0.731383458715156, 0.660722525562598, 0.583647507841499, 0.500906625360716, 0.413303100345869, 0.321687360012960, 0.226948780890808, 0.130007055036036, 0.0318032619544833, -0.0667092670999465, -0.164574203762531,
-0.260841506289903, -0.354576642263842, -0.444869660735319, -0.530844025739510, -0.611665125428735, -0.686548374219374, -0.754766829299691, -0.815658247559921, -0.868631514438194, -0.913172382273058, -0.948848462456602, -0.975313422925793, -0.992310350244224, -0.999674243636218, -0.997333616761983, -0.985311191684313,
-0.963723678290009, -0.932780641307352, -0.892782465918221, -0.844117441714098, -0.787257993303952, -0.722756094166077, -0.651237908264765, -0.573397711449273, -0.489991151644234, -0.401827913259485, -0.309763857030723, -0.214692711595228, -0.117537397457843, -0.0192410675716325, 0.0792420484915564, 0.176955907898970,
0.272951935517313, 0.366298232379078, 0.456088622261896, 0.541451448559776, 0.621558036048674, 0.695630735402642, 0.762950472366612, 0.822863728301042, 0.874788884333453, 0.918221867529835, 0.952741044274703, 0.978011313356413, 0.993787359023354, 0.999916032431425, 0.996337838364386, 0.983087512794541,
0.960293685676943, 0.928177632250618, 0.887051124968773, 0.837313406910770, 0.779447316057058, 0.714014598051266, 0.641650452951816, 0.563057368911361, 0.478998302644763, 0.390289272887461, 0.297791438744591, 0.202402739831719, 0.105049179362040, 0.00667583480114843, -0.0917623166451206, -0.189309668646243,
-0.285019262469973, -0.377961979757159, -0.467235562107885, -0.551973369919598, -0.631352795449380, -0.704603248428773, -0.771013636709533, -0.829939269320517, -0.880808114923002, -0.923126354909364, -0.956483177237392, -0.980554764463184, -0.995107437258145, -0.999999922893717, -0.995184726672197, -0.980708592990188,
-0.956712051558832, -0.923428053185976, -0.881179708363663, -0.830377150614053, -0.771513555043328, -0.705160350754312, -0.631961673594109, -0.552628113087742, -0.467929814260579, -0.378689001314870, -0.285771995738146, -0.190080806329236, -0.0925443727830621, 0.00589045216103947, 0.104268094465144, 0.201633535202275,
0.297041581577028, 0.389566042557161, 0.478308720042225, 0.562408128285632, 0.641047856924812, 0.713464496433956, 0.778955049061354, 0.836883753308413, 0.886688255700559, 0.927885069936238, 0.960074270418487, 0.982943374605696, 0.996270376492941, 0.999925901775817, 0.993874463775118, 0.978174807930265,
0.952979341517217, 0.918532654127013, 0.875169143267650, 0.823309768139218, 0.763457963095587, 0.696194750463496, 0.622173100162199, 0.542111590878786, 0.456787434334296, 0.367028930359057, 0.273707426020814, 0.177728856863960, 0.0800249523748130, -0.0184558089525184, -0.116757407144309, -0.213925561485702,
-0.309016994374950, -0.401108588363064, -0.489306347484964, -0.572754075888567, -0.650641689512372, -0.722213080124420, -0.786773455380986, -0.843696083650190, -0.892428378123718, -0.932497261154178, -0.963513756742748, -0.985176766594716, -0.997275993086157, -0.999693980766518, -0.992407256578852, -0.975486557728811,
-0.949096144990295, -0.913492208114000, -0.869020378818633, -0.816112375507883, -0.755281812285189, -0.687119212951036, -0.612286278385335, -0.531509462965285, -0.445572922376897, -0.355310901280419, -0.261599634727995, -0.165348841951888, -0.0674928950989053, 0.0310182513578315, 0.129228282475433, 0.226183806443067,
0.320943609807221, 0.412587794473066, 0.500226707783204, 0.583009578983345, 0.660132778234681, 0.730847617997353, 0.794467621051437, 0.850375184599931, 0.898027575760620, 0.936962200244736, 0.966801093075418, 0.987254587751762, 0.998124128239275, 0.999304196488835, 0.990783336772423, 0.972644266891692,
0.945063075179802, 0.908307511091783, 0.862734385977789, 0.808786109271773, 0.746986393721116, 0.677935171349503, 0.602302769507245, 0.520823403546116, 0.434288049289797, 0.343536764491561, 0.249450533820281, 0.152942716540872, 0.0549501799124448, -0.0435757956216944, -0.141678751162651, -0.238406334355361,
-0.332819544522994, -0.424001848187439, -0.511068076485486, -0.593173018107150, -0.669519624338816, -0.739366746559169, -0.802036331074781, -0.856920001450147, -0.903484964433038, -0.941279182142260, -0.969935760307973, -0.989176509964781, -0.998814648021921, -0.998756610494226, -0.989002960791592, -0.969648384249581,
-0.940880768954224, -0.902979381784135, -0.856312157376272, -0.801332126333271, -0.738573017346144, -0.668644075925549, -0.592224150039745, -0.510055100073975, -0.422934597085312, -0.331708379265198, -0.237262041781567, -0.140512439701961, -0.0423987874555876, 0.0561264587622677, 0.154106847132682, 0.250591215143780,
0.344642923174507, 0.435348947094810, 0.521828741614001, 0.603242788335117, 0.678800745532949, 0.747769120540683, 0.809478390263939, 0.863329500698409, 0.908799682356035, 0.945447525145274, 0.972917263440128, 0.990942229739981, 0.999347443393019, 0.998051309252864, 0.987066409778365, 0.966499382887074,
0.936549886748192, 0.897508661564435, 0.849754707158412, 0.793751603762675, 0.730043011729913, 0.659247393850829, 0.582052011513663, 0.499206252988763, 0.411514358605109, 0.319827613440464, 0.225036083316014, 0.128059974319935, 0.0298406997386746, -0.0686682588843781, -0.166510607845101, -0.262736524674396,
-0.356411878713249, -0.446627299356684, -0.532507003934973, -0.613217299533782, -0.687974676220444, -0.756053413109658, -0.816792623431512, -0.869602670210485, -0.913970890274060, -0.949466571024084, -0.975745131657964, -0.992551468249736, -0.999722430218001, -0.997188404139995, -0.984973989536599, -0.963197760067995,
-0.932071112458212, -0.891896214322823, -0.843063070821639, -0.786045738612394, -0.721397723859212, -0.649746608970392, -0.571787960227613, -0.488278575449232, -0.400029137237269, -0.307896343128008, -0.212774589044232, -0.115587286783459, -0.0172778998293627, 0.0811992154923688, 0.178888074602276, 0.274840345062014
};
float freq;
float ind;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
#if 1
printf("Freq Compute start\n");
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
a = val;
}
}
printf("Freq Compute over\n");
#endif
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}
正在编写一个简单的 (vanila) DFT 程序并观察到异常行为:
重新创建
编译以下程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate 2*pi*50*t sine wave in range (-1,+1)
float y[] = {
0.0, 0.0984079408900634, 0.195860568724094, 0.291411844321996, 0.384134186227703, 0.473127475375545, 0.557527793160445, 0.636515808085535, 0.709324729572274, 0.775247751720011, 0.833644914753334, 0.883949317548447, 0.925672620929349, 0.958409788309562, 0.981843017658750, 0.995744826623968,
0.999980260856137, 0.994508204104035, 0.979381777357810, 0.954747823167273, 0.920845480141026, 0.878003861464755, 0.826638859974879, 0.767249110802900, 0.700411150783807, 0.626773821619470, 0.547051971129342, 0.462019513734716, 0.372501917543151, 0.279368190966096, 0.183522446661037, 0.0858951246927854,
-0.0125660398833526, -0.110905217332694, -0.208167762132862, -0.303409480369524, -0.395705795671223, -0.484160724703344, -0.567915575089941, -0.646157281326779, -0.718126297763189, -0.783123972030169, -0.840519327335832, -0.889755187787833, -0.930353587280090, -0.961920409436037, -0.984149213565316, -0.996824209492737,
-0.999822352380809, -0.993114537209969, -0.976765881320872, -0.950935092275919, -0.915872927176609, -0.871919758393194, -0.819502269337733, -0.759129312356960, -0.691386968955206, -0.616932860301177, -0.536489763250371, -0.450838593856768, -0.360810826487642, -0.267280422134304, -0.171155344276191, -0.0733687446595907,
0.0251300954433374, 0.123384980555854, 0.220442083438833, 0.315359204542867, 0.407214918582441, 0.495117519440145, 0.578213676566644, 0.655696718842807, 0.726814465486903, 0.790876527986881, 0.847261012172933, 0.895420555368697, 0.934887640014398, 0.965279132178928, 0.986300000902440, 0.997746182263274,
0.999506560365732, 0.991564046017929, 0.973995742628244, 0.946972197709905, 0.910755747259856, 0.865697969084168, 0.812236269792286, 0.750889638592492, 0.682253609109396, 0.606994478132532, 0.525842837418596, 0.439586481338564, 0.349062759219976, 0.255150446625443, 0.158761214478388, 0.0608307788510959,
-0.0376901826699336, -0.135845259860259, -0.232681594384382, -0.327259129842054, -0.418659737537428, -0.505996129381040, -0.588420471400950, -0.665132614245854, -0.735387860781017, -0.798504195371694, -0.853868904674451, -0.900944525663825, -0.939274063152787, -0.968485426156976, -0.988295040035869, -0.998510599345391,
-0.999032934678125, -0.989856975368447, -0.971071798716938, -0.942859765256222, -0.905494748452794, -0.859339476030335, -0.804842008724098, -0.742531390649502, -0.673012513509774, -0.596960244499286, -0.515112874907027, -0.428264953019107, -0.337259570896168, -0.242980179903264, -0.146342014444403, -0.0482832071574495,
0.0502443181797695, 0.148284087623366, 0.244884362208849, 0.339107377130930, 0.430038445266602, 0.516794836667414, 0.598534347821637, 0.674463477498649, 0.743845129807025, 0.806005769687504, 0.860341961377560, 0.906326226374270, 0.943512164028194, 0.971538785059195, 0.990134015925798, 0.999117340028769,
0.998401550108975, 0.987993594827769, 0.967994511311594, 0.938598444315629, 0.900090761528124, 0.852845283311457, 0.797320653772707, 0.734055888392230, 0.663665141432458, 0.586831743923250, 0.504301570101134, 0.416875796698963, 0.325403125376477, 0.230771543793960, 0.133899705309884, 0.0357280109856745,
-0.0627905195293142, -0.160699499610071, -0.257048459953733, -0.350902075433899, -0.441349244940104, -0.527511936058200, -0.608553708730373, -0.683687835149718, -0.752184937064112, -0.813380066348762, -0.866679160111565, -0.911564807667102, -0.947601273395360, -0.974438726724819, -0.991816638176614, -0.999566308502021,
-0.997612506361225, -0.985974198645282, -0.964764366351565, -0.934188907800099, -0.894544639838025, -0.846216416435848, -0.789673392647252, -0.725464470200735, -0.654212968935859, -0.576610575812079, -0.493410630231271, -0.405420810857945, -0.313495294931096, -0.218526466182705, -0.121436251859683, -0.0231671729467908,
0.0753268055279309, 0.173089535282900, 0.269171966766988, 0.362641362231371, 0.452590350451546, 0.538145735199171, 0.618476971953920, 0.692804230566056, 0.760405965600031, 0.820625920868545, 0.872879500159325, 0.916659442309612, 0.951540745536515, 0.977184793219440, 0.993342641082758, 0.999857433867821,
0.996665928034030, 0.983799105707054, 0.961381873914185, 0.929631852026567, 0.888857259179404, 0.839453922178432, 0.781901432938911, 0.716758492759546, 0.644657488627590, 0.566298354206707, 0.482441775103100, 0.393901804371106, 0.301537959944494, 0.206246880709201, 0.108953622217581, 0.0106026765427436,
-0.0878511965507475, -0.185452238111593, -0.281252968206354, -0.374323383753877, -0.463759986700061, -0.548694554890174, -0.628302570493986, -0.701811224163148, -0.768506917219076, -0.827742189042424, -0.878942002415270, -0.921609325799925, -0.955329958363339, -0.979776550907327, -0.994711783670682, -0.999990670154097,
-0.995561964603080, -0.981468659485473, -0.957847568134224, -0.924927996606965, -0.883029517655600, -0.832558868415444, -0.774006001930223, -0.707939330843420, -0.635000209428760, -0.555896707526464, -0.471396736825997, -0.382320596223098, -0.289533008618479, -0.193934726462339, -0.0964537875354998, 0.00196349414684702,
0.100361714851215, 0.197785655882052, 0.293289556541649, 0.385946295274789, 0.474856389870597, 0.559156729350287, 0.638028952774654, 0.710707393632289, 0.776486512687080, 0.834727747129162, 0.884865709540016, 0.926413676494061, 0.958968313515203, 0.982213590519895, 0.995923849736900, 0.999965996321297,
0.994300790396999, 0.978983227985015, 0.954162007119530, 0.920078084334595, 0.877062335534554, 0.825532343955807, 0.765988346401266, 0.699008377100263, 0.625242656335702, 0.545407278311946, 0.460277259539545, 0.370679015220949, 0.277482336674029, 0.181591947674010, 0.0839387216822394, -0.0145293547779467,
-0.112856384873481, -0.210087841004649, -0.305279831056042, -0.397508261401654, -0.485877807712459, -0.569530606480889, -0.647654582887413, -0.719491334165196, -0.784343491933411, -0.841581492028159, -0.890649686111535, -0.931071735729352, -0.962455236453648, -0.984495527220340, -0.996978647882129, -0.999783416265704,
-0.992882604569814, -0.976343203684123, -0.950325772862918, -0.915082881066822, -0.870956655103499, -0.818375458369187, -0.757849732432795, -0.689967041831200, -0.615386370179169, -0.534831722965621, -0.449085099138105, -0.358978899705249, -0.265387847051938, -0.169220493412062, -0.0714104009317734, 0.0270929210554974,
0.125333233564306, 0.222356850821733, 0.317221898346202, 0.409007456365973, 0.496822499816019, 0.579814548126516, 0.657177940833683, 0.728161658675826, 0.792076614249970, 0.848302341453640, 0.896293018772879, 0.935582767944242, 0.965790176553122, 0.986622000664402, 0.997876011541515, 0.999442958818825,
0.991307631069506, 0.973549003473241, 0.946339471150244, 0.909943175604153, 0.864713440520154, 0.811089341810776, 0.749591445206302, 0.680816752767877, 0.605432907381000, 0.524171711490274, 0.437822022993559, 0.347222097259867, 0.253251449612327, 0.156822317272532, 0.0588708036511916, -0.0396522090467458,
-0.137790290684639, -0.234590747914437, -0.329113872621261, -0.420442064311573, -0.507688737887499, -0.590006930333573, -0.666597522764850, -0.736716998019437, -0.799684658487090, -0.854889234105571, -0.901794816376402, -0.939946060794457, -0.968972607187923, -0.988592675057273, -0.998615799010932, -0.998944677742839,
-0.989576118602650, -0.970601068588971, -0.942203731464759, -0.904659779565662, -0.858333677660472, -0.803675144842848, -0.741214788801068, -0.671558954847019, -0.595383839708351, -0.513428927225288, -0.426489809676181, -0.335410464420208, -0.241075060833035, -0.144399377071172, -0.0463219099882471, 0.0522052354945227,
0.150225589120753, 0.246787600408572, 0.340953876000616, 0.431810279581299, 0.518474806021958, 0.600106143606748, 0.675911841219724, 0.745156001208767, 0.807166423246402, 0.861341129837251, 0.907154210124478, 0.944160925265473, 0.972002025815363, 0.990407239206624, 0.999197893469363, 0.998288651722103,
0.987688340595138, 0.967499864544408, 0.937919206887704, 0.899233527260826, 0.851818373962972, 0.796134038253055, 0.732721085988247, 0.662195109982227, 0.585240754025510, 0.502605066580843, 0.415090248673835, 0.323545866380009, 0.228860603507013, 0.131953634534243, 0.0337657015587562, -0.0647500181304328,
-0.162637165194890, -0.258945482279751, -0.352740038810489, -0.443110307002175, -0.529179000974195, -0.610110593163201, -0.685119425359456, -0.753477335627384, -0.814520727070512, -0.867657009819546, -0.912370353706714, -0.948226695781328, -0.974877954055122, -0.992065406571739, -0.999622202997347, -0.997474984350731,
-0.985644595148997, -0.964245881055618, -0.933486573995179, -0.893665275557789, -0.845168558269628, -0.788467212869540, -0.724111678021981, -0.652726696833165, -0.575005252043276, -0.491701838769995, -0.403625140109338, -0.311630176696852, -0.216610006438655, -0.119487054988758, -0.0212041611335505, 0.0772845759878599,
0.175023058975285, 0.271062473657499, 0.364470499879154, 0.454340362168866, 0.539799632427741, 0.620018699184368, 0.694218821199767, 0.761679687240114, 0.821746408629595, 0.873835876701791, 0.917442423433567, 0.952142730309724, 0.977599937764792, 0.993566915308769, 0.999888660591489, 0.996503804116233,
0.983445204995330, 0.960839631964316, 0.928906532751298, 0.887955903748049, 0.838385280663432, 0.780675879372910, 0.715387924427582, 0.643155210572020, 0.564678950066079, 0.480720965538817, 0.392096294456247, 0.299665276996257, 0.204325204139264, 0.107001607052153, 0.00863927232549537, -0.0898069297148414,
-0.187381314585722, -0.283136661128381, -0.376143406830793, -0.465498671725509, -0.550335023261759, -0.629828897065438, -0.703208591840560, -0.769761760800563, -0.828842326904764, -0.879876754769274, -0.922369618366427, -0.955908410463418, -0.980167547111590, -0.994911528312078, -0.999997224175046, -0.995375264379227,
-0.981090517443334, -0.957281655156723, -0.924179806397673, -0.882106313407595, -0.831469612302545, -0.772761268105050, -0.706551202786880, -0.633482162647440, -0.554263478736693, -0.469664180894493, -0.380505532252943, -0.287653056674601, -0.192008136521520, -0.0944992623213991, 0.00392698072380246, 0.102315101886539,
0.199709980514407, 0.295166138038260, 0.387756916378113, 0.476583473645635, 0.560783509815936, 0.639539637662464, 0.712087317692860, 0.777722280055610, 0.835807361368273, 0.885778690097328, 0.927151160444101, 0.959523141597856, 0.982580376640236, 0.996099033251688, 0.999947876604571, 0.994089543349219,
0.978580904325473, 0.953572512478617, 0.919307141339182, 0.876117428254551, 0.824422645251177, 0.764724628874819, 0.697602908520652, 0.623709080545813, 0.543760482778796, 0.458533230831479, 0.368854683815130, 0.275595412600709, 0.179660748593197, 0.0819819950616476, -0.0164926136573281, -0.114807117317581,
-0.212007109922050, -0.307149004793269, -0.399309194613317, -0.487593017510555, -0.571143442153087, -0.649149387536929, -0.720853596702917, -0.785559987947003, -0.842640412160429, -0.891540750701937, -0.931786294605665, -0.962986352905092, -0.984838045336978, -0.997129242606799, -0.999740625672616, -0.992646844056464,
-0.975916761938747, -0.949712789646630, -0.914289307026130, -0.869990194003309, -0.817245492307110, -0.756567230760720, -0.688544454668300, -0.613837507550104, -0.533171620737190, -0.447329873055838, -0.357145588946850, -0.263494248816364, -0.167284990149970, -0.0694517818945103, 0.0290556422159688, 0.127281003373913,
0.224270760949379, 0.319083369159829, 0.410798417297745, 0.498525564785646, 0.581413184319829, 0.658656629197908, 0.729506044573661, 0.793273646809804, 0.849340400263315, 0.897162026686948, 0.936274288909695, 0.966297497503909, 0.986940196689757, 0.998001993695415, 0.999375504106507, 0.991047394319906,
0.973098510982126, 0.945703096155735, 0.909127095832718, 0.863725578215192, 0.809939286825997, 0.748290361910482, 0.679377271664694, 0.603869002496122, 0.522498564715948, 0.436055876707622, 0.345380096649905, 0.251351476235615, 0.154882815467577, 0.0569106014859085, -0.0416140825518728, -0.139734790284324,
-0.236498997023725, -0.330967346563474, -0.422222770149962, -0.509379389094972, -0.591591114604805, -0.668059861341758, -0.738043294983249, -0.800862038567848, -0.855906267671136, -0.902641630387714, -0.940614434649876, -0.969456052526198, -0.988886498744503, -0.998717148700020, -0.998852569563171, -0.989291446711198,
-0.970126596490105, -0.941544065183020, -0.903821322931938, -0.857324570145646, -0.802505182542385, -0.739895329337642, -0.670102807114307, -0.593805139526380, -0.511743000114341, -0.424713022081554, -0.333560064831927, -0.239169012343003, -0.142456182993156, -0.0443604342336034, 0.0541659515417433, 0.152166511451484,
0.248689887164858, 0.342799060386402, 0.433580449132212, 0.520152776493846, 0.601675625794849, 0.677357599089101, 0.746463999800899, 0.808323964926108, 0.862336977557309, 0.907978696511290, 0.944806046466880, 0.972461519199504, 0.990676644157565, 0.999274594689349, 0.998171904620035, 0.987379278514827,
0.967001487762437, 0.937236353487686, 0.898372826166838, 0.850788180588052, 0.794944353387510, 0.731383458715156, 0.660722525562598, 0.583647507841499, 0.500906625360716, 0.413303100345869, 0.321687360012960, 0.226948780890808, 0.130007055036036, 0.0318032619544833, -0.0667092670999465, -0.164574203762531,
-0.260841506289903, -0.354576642263842, -0.444869660735319, -0.530844025739510, -0.611665125428735, -0.686548374219374, -0.754766829299691, -0.815658247559921, -0.868631514438194, -0.913172382273058, -0.948848462456602, -0.975313422925793, -0.992310350244224, -0.999674243636218, -0.997333616761983, -0.985311191684313,
-0.963723678290009, -0.932780641307352, -0.892782465918221, -0.844117441714098, -0.787257993303952, -0.722756094166077, -0.651237908264765, -0.573397711449273, -0.489991151644234, -0.401827913259485, -0.309763857030723, -0.214692711595228, -0.117537397457843, -0.0192410675716325, 0.0792420484915564, 0.176955907898970,
0.272951935517313, 0.366298232379078, 0.456088622261896, 0.541451448559776, 0.621558036048674, 0.695630735402642, 0.762950472366612, 0.822863728301042, 0.874788884333453, 0.918221867529835, 0.952741044274703, 0.978011313356413, 0.993787359023354, 0.999916032431425, 0.996337838364386, 0.983087512794541,
0.960293685676943, 0.928177632250618, 0.887051124968773, 0.837313406910770, 0.779447316057058, 0.714014598051266, 0.641650452951816, 0.563057368911361, 0.478998302644763, 0.390289272887461, 0.297791438744591, 0.202402739831719, 0.105049179362040, 0.00667583480114843, -0.0917623166451206, -0.189309668646243,
-0.285019262469973, -0.377961979757159, -0.467235562107885, -0.551973369919598, -0.631352795449380, -0.704603248428773, -0.771013636709533, -0.829939269320517, -0.880808114923002, -0.923126354909364, -0.956483177237392, -0.980554764463184, -0.995107437258145, -0.999999922893717, -0.995184726672197, -0.980708592990188,
-0.956712051558832, -0.923428053185976, -0.881179708363663, -0.830377150614053, -0.771513555043328, -0.705160350754312, -0.631961673594109, -0.552628113087742, -0.467929814260579, -0.378689001314870, -0.285771995738146, -0.190080806329236, -0.0925443727830621, 0.00589045216103947, 0.104268094465144, 0.201633535202275,
0.297041581577028, 0.389566042557161, 0.478308720042225, 0.562408128285632, 0.641047856924812, 0.713464496433956, 0.778955049061354, 0.836883753308413, 0.886688255700559, 0.927885069936238, 0.960074270418487, 0.982943374605696, 0.996270376492941, 0.999925901775817, 0.993874463775118, 0.978174807930265,
0.952979341517217, 0.918532654127013, 0.875169143267650, 0.823309768139218, 0.763457963095587, 0.696194750463496, 0.622173100162199, 0.542111590878786, 0.456787434334296, 0.367028930359057, 0.273707426020814, 0.177728856863960, 0.0800249523748130, -0.0184558089525184, -0.116757407144309, -0.213925561485702,
-0.309016994374950, -0.401108588363064, -0.489306347484964, -0.572754075888567, -0.650641689512372, -0.722213080124420, -0.786773455380986, -0.843696083650190, -0.892428378123718, -0.932497261154178, -0.963513756742748, -0.985176766594716, -0.997275993086157, -0.999693980766518, -0.992407256578852, -0.975486557728811,
-0.949096144990295, -0.913492208114000, -0.869020378818633, -0.816112375507883, -0.755281812285189, -0.687119212951036, -0.612286278385335, -0.531509462965285, -0.445572922376897, -0.355310901280419, -0.261599634727995, -0.165348841951888, -0.0674928950989053, 0.0310182513578315, 0.129228282475433, 0.226183806443067,
0.320943609807221, 0.412587794473066, 0.500226707783204, 0.583009578983345, 0.660132778234681, 0.730847617997353, 0.794467621051437, 0.850375184599931, 0.898027575760620, 0.936962200244736, 0.966801093075418, 0.987254587751762, 0.998124128239275, 0.999304196488835, 0.990783336772423, 0.972644266891692,
0.945063075179802, 0.908307511091783, 0.862734385977789, 0.808786109271773, 0.746986393721116, 0.677935171349503, 0.602302769507245, 0.520823403546116, 0.434288049289797, 0.343536764491561, 0.249450533820281, 0.152942716540872, 0.0549501799124448, -0.0435757956216944, -0.141678751162651, -0.238406334355361,
-0.332819544522994, -0.424001848187439, -0.511068076485486, -0.593173018107150, -0.669519624338816, -0.739366746559169, -0.802036331074781, -0.856920001450147, -0.903484964433038, -0.941279182142260, -0.969935760307973, -0.989176509964781, -0.998814648021921, -0.998756610494226, -0.989002960791592, -0.969648384249581,
-0.940880768954224, -0.902979381784135, -0.856312157376272, -0.801332126333271, -0.738573017346144, -0.668644075925549, -0.592224150039745, -0.510055100073975, -0.422934597085312, -0.331708379265198, -0.237262041781567, -0.140512439701961, -0.0423987874555876, 0.0561264587622677, 0.154106847132682, 0.250591215143780,
0.344642923174507, 0.435348947094810, 0.521828741614001, 0.603242788335117, 0.678800745532949, 0.747769120540683, 0.809478390263939, 0.863329500698409, 0.908799682356035, 0.945447525145274, 0.972917263440128, 0.990942229739981, 0.999347443393019, 0.998051309252864, 0.987066409778365, 0.966499382887074,
0.936549886748192, 0.897508661564435, 0.849754707158412, 0.793751603762675, 0.730043011729913, 0.659247393850829, 0.582052011513663, 0.499206252988763, 0.411514358605109, 0.319827613440464, 0.225036083316014, 0.128059974319935, 0.0298406997386746, -0.0686682588843781, -0.166510607845101, -0.262736524674396,
-0.356411878713249, -0.446627299356684, -0.532507003934973, -0.613217299533782, -0.687974676220444, -0.756053413109658, -0.816792623431512, -0.869602670210485, -0.913970890274060, -0.949466571024084, -0.975745131657964, -0.992551468249736, -0.999722430218001, -0.997188404139995, -0.984973989536599, -0.963197760067995,
-0.932071112458212, -0.891896214322823, -0.843063070821639, -0.786045738612394, -0.721397723859212, -0.649746608970392, -0.571787960227613, -0.488278575449232, -0.400029137237269, -0.307896343128008, -0.212774589044232, -0.115587286783459, -0.0172778998293627, 0.0811992154923688, 0.178888074602276, 0.274840345062014
};
float freq=0;
float ind=0;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
printf("Freq Compute start\n");
/*
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
}
}
printf("Freq Compute over\n");
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
*/
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}
具有以下编译器标志:
gcc -O3 DFT.c -o DFT_try -lm -lrt
此程序在 BeagleBone Black 上 运行 仅需 3-5 us(微秒),在 PC 上甚至更少。和现在一样,
如果您取消注释下面的 "Freq compute" 部分:
for (k=0;k<2000;k++)
{
val = (Xre[k]*Xre[k])+(Xim[k]*Xim[k]);
if(val> maxV){
printf("%f",val);
maxV = val;
ind = k;
a=val;
}
}
使代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate 2*pi*50*t sine wave in range (-1,+1)
float y[] = {
//<Copy data HERE, from above program };
float freq=0;
float ind=0;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
printf("Freq Compute start\n");
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
}
}
printf("Freq Compute over\n");
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}
然后重新编译,如上
这个程序将花费 16198709 uSec 甚至更多的时间到 BeagleBone Black 上的 运行 和大约 0.001 秒如 chqrlie 在评论中所述,在 64 位现代系统上
谁能告诉我为什么会出现这种奇怪的行为?
当你注释代码块时,编译器可以确定你没有使用第一个循环中的任何计算结果并删除所有代码,留下一个简单的输出语句:
maxV = 0;
ind = 0;
freq = 0;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
如果您取消注释循环,ind
是根据第一个循环中计算的复数值 Xre[k]
和 Xim[k]
计算的,因此它也必须计算这些,因此需要更长的时间计算时间,虽然没有你提到的那么长(我的笔记本电脑上是 1s)。
请注意,更积极的优化器会确定可以在编译时执行计算,并生成一个在两种情况下执行速度都一样快的简单程序。为了避免潜在的无限编译时间,这样的编译器会设置超时并在一段时间后放弃,但在我的笔记本电脑上只需要 1 秒就可以 运行 完整代码。
你的代码问题较多:
您应该为 2*PI 使用更精确的值:2 * M_PI.
y
数组中只有 960 个值。x
数组中的条目一直到 16383 都是零。可以简化计算:内部循环可以在y
. 的大小处停止
你在第一个循环中计算了
P[k]
但你没有在第二个循环中使用它。
如果将 N
的值减小到 y
的大小,计算速度会快得多。它 运行 比我的系统快 20 倍:
maxV=228694.692537 ind = 257.000000 Freq= 50.115000
2115850.697709000 47955 uSec
考虑到 Beaglebone 系统与 64 位现代 PC 之间的巨大差异,嵌入式处理器似乎没有浮点单元,使得计算时间 很多 更慢,甚至 float
。请注意,在 PC 上,由于转换次数较少,double
的计算实际上比 float
快得多。
这是更新后的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <complex.h>
#define N 16384
#define PI2 (2 * M_PI) //6.2832
int main(void) {
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
float a = 1.09; // unused: b = 2.098;
float Xre[2000], Xim[2000]; // DFT of x (real and imaginary parts)
float P[2000]; // power spectrum of x
float x[N] = { 0 };
// Generate random discrete-time signal x in range (-1,+1)
float y[] = {
0.0, 0.0984079408900634, 0.195860568724094, 0.291411844321996, 0.384134186227703, 0.473127475375545, 0.557527793160445, 0.636515808085535, 0.709324729572274, 0.775247751720011, 0.833644914753334, 0.883949317548447, 0.925672620929349, 0.958409788309562, 0.981843017658750, 0.995744826623968,
0.999980260856137, 0.994508204104035, 0.979381777357810, 0.954747823167273, 0.920845480141026, 0.878003861464755, 0.826638859974879, 0.767249110802900, 0.700411150783807, 0.626773821619470, 0.547051971129342, 0.462019513734716, 0.372501917543151, 0.279368190966096, 0.183522446661037, 0.0858951246927854,
-0.0125660398833526, -0.110905217332694, -0.208167762132862, -0.303409480369524, -0.395705795671223, -0.484160724703344, -0.567915575089941, -0.646157281326779, -0.718126297763189, -0.783123972030169, -0.840519327335832, -0.889755187787833, -0.930353587280090, -0.961920409436037, -0.984149213565316, -0.996824209492737,
-0.999822352380809, -0.993114537209969, -0.976765881320872, -0.950935092275919, -0.915872927176609, -0.871919758393194, -0.819502269337733, -0.759129312356960, -0.691386968955206, -0.616932860301177, -0.536489763250371, -0.450838593856768, -0.360810826487642, -0.267280422134304, -0.171155344276191, -0.0733687446595907,
0.0251300954433374, 0.123384980555854, 0.220442083438833, 0.315359204542867, 0.407214918582441, 0.495117519440145, 0.578213676566644, 0.655696718842807, 0.726814465486903, 0.790876527986881, 0.847261012172933, 0.895420555368697, 0.934887640014398, 0.965279132178928, 0.986300000902440, 0.997746182263274,
0.999506560365732, 0.991564046017929, 0.973995742628244, 0.946972197709905, 0.910755747259856, 0.865697969084168, 0.812236269792286, 0.750889638592492, 0.682253609109396, 0.606994478132532, 0.525842837418596, 0.439586481338564, 0.349062759219976, 0.255150446625443, 0.158761214478388, 0.0608307788510959,
-0.0376901826699336, -0.135845259860259, -0.232681594384382, -0.327259129842054, -0.418659737537428, -0.505996129381040, -0.588420471400950, -0.665132614245854, -0.735387860781017, -0.798504195371694, -0.853868904674451, -0.900944525663825, -0.939274063152787, -0.968485426156976, -0.988295040035869, -0.998510599345391,
-0.999032934678125, -0.989856975368447, -0.971071798716938, -0.942859765256222, -0.905494748452794, -0.859339476030335, -0.804842008724098, -0.742531390649502, -0.673012513509774, -0.596960244499286, -0.515112874907027, -0.428264953019107, -0.337259570896168, -0.242980179903264, -0.146342014444403, -0.0482832071574495,
0.0502443181797695, 0.148284087623366, 0.244884362208849, 0.339107377130930, 0.430038445266602, 0.516794836667414, 0.598534347821637, 0.674463477498649, 0.743845129807025, 0.806005769687504, 0.860341961377560, 0.906326226374270, 0.943512164028194, 0.971538785059195, 0.990134015925798, 0.999117340028769,
0.998401550108975, 0.987993594827769, 0.967994511311594, 0.938598444315629, 0.900090761528124, 0.852845283311457, 0.797320653772707, 0.734055888392230, 0.663665141432458, 0.586831743923250, 0.504301570101134, 0.416875796698963, 0.325403125376477, 0.230771543793960, 0.133899705309884, 0.0357280109856745,
-0.0627905195293142, -0.160699499610071, -0.257048459953733, -0.350902075433899, -0.441349244940104, -0.527511936058200, -0.608553708730373, -0.683687835149718, -0.752184937064112, -0.813380066348762, -0.866679160111565, -0.911564807667102, -0.947601273395360, -0.974438726724819, -0.991816638176614, -0.999566308502021,
-0.997612506361225, -0.985974198645282, -0.964764366351565, -0.934188907800099, -0.894544639838025, -0.846216416435848, -0.789673392647252, -0.725464470200735, -0.654212968935859, -0.576610575812079, -0.493410630231271, -0.405420810857945, -0.313495294931096, -0.218526466182705, -0.121436251859683, -0.0231671729467908,
0.0753268055279309, 0.173089535282900, 0.269171966766988, 0.362641362231371, 0.452590350451546, 0.538145735199171, 0.618476971953920, 0.692804230566056, 0.760405965600031, 0.820625920868545, 0.872879500159325, 0.916659442309612, 0.951540745536515, 0.977184793219440, 0.993342641082758, 0.999857433867821,
0.996665928034030, 0.983799105707054, 0.961381873914185, 0.929631852026567, 0.888857259179404, 0.839453922178432, 0.781901432938911, 0.716758492759546, 0.644657488627590, 0.566298354206707, 0.482441775103100, 0.393901804371106, 0.301537959944494, 0.206246880709201, 0.108953622217581, 0.0106026765427436,
-0.0878511965507475, -0.185452238111593, -0.281252968206354, -0.374323383753877, -0.463759986700061, -0.548694554890174, -0.628302570493986, -0.701811224163148, -0.768506917219076, -0.827742189042424, -0.878942002415270, -0.921609325799925, -0.955329958363339, -0.979776550907327, -0.994711783670682, -0.999990670154097,
-0.995561964603080, -0.981468659485473, -0.957847568134224, -0.924927996606965, -0.883029517655600, -0.832558868415444, -0.774006001930223, -0.707939330843420, -0.635000209428760, -0.555896707526464, -0.471396736825997, -0.382320596223098, -0.289533008618479, -0.193934726462339, -0.0964537875354998, 0.00196349414684702,
0.100361714851215, 0.197785655882052, 0.293289556541649, 0.385946295274789, 0.474856389870597, 0.559156729350287, 0.638028952774654, 0.710707393632289, 0.776486512687080, 0.834727747129162, 0.884865709540016, 0.926413676494061, 0.958968313515203, 0.982213590519895, 0.995923849736900, 0.999965996321297,
0.994300790396999, 0.978983227985015, 0.954162007119530, 0.920078084334595, 0.877062335534554, 0.825532343955807, 0.765988346401266, 0.699008377100263, 0.625242656335702, 0.545407278311946, 0.460277259539545, 0.370679015220949, 0.277482336674029, 0.181591947674010, 0.0839387216822394, -0.0145293547779467,
-0.112856384873481, -0.210087841004649, -0.305279831056042, -0.397508261401654, -0.485877807712459, -0.569530606480889, -0.647654582887413, -0.719491334165196, -0.784343491933411, -0.841581492028159, -0.890649686111535, -0.931071735729352, -0.962455236453648, -0.984495527220340, -0.996978647882129, -0.999783416265704,
-0.992882604569814, -0.976343203684123, -0.950325772862918, -0.915082881066822, -0.870956655103499, -0.818375458369187, -0.757849732432795, -0.689967041831200, -0.615386370179169, -0.534831722965621, -0.449085099138105, -0.358978899705249, -0.265387847051938, -0.169220493412062, -0.0714104009317734, 0.0270929210554974,
0.125333233564306, 0.222356850821733, 0.317221898346202, 0.409007456365973, 0.496822499816019, 0.579814548126516, 0.657177940833683, 0.728161658675826, 0.792076614249970, 0.848302341453640, 0.896293018772879, 0.935582767944242, 0.965790176553122, 0.986622000664402, 0.997876011541515, 0.999442958818825,
0.991307631069506, 0.973549003473241, 0.946339471150244, 0.909943175604153, 0.864713440520154, 0.811089341810776, 0.749591445206302, 0.680816752767877, 0.605432907381000, 0.524171711490274, 0.437822022993559, 0.347222097259867, 0.253251449612327, 0.156822317272532, 0.0588708036511916, -0.0396522090467458,
-0.137790290684639, -0.234590747914437, -0.329113872621261, -0.420442064311573, -0.507688737887499, -0.590006930333573, -0.666597522764850, -0.736716998019437, -0.799684658487090, -0.854889234105571, -0.901794816376402, -0.939946060794457, -0.968972607187923, -0.988592675057273, -0.998615799010932, -0.998944677742839,
-0.989576118602650, -0.970601068588971, -0.942203731464759, -0.904659779565662, -0.858333677660472, -0.803675144842848, -0.741214788801068, -0.671558954847019, -0.595383839708351, -0.513428927225288, -0.426489809676181, -0.335410464420208, -0.241075060833035, -0.144399377071172, -0.0463219099882471, 0.0522052354945227,
0.150225589120753, 0.246787600408572, 0.340953876000616, 0.431810279581299, 0.518474806021958, 0.600106143606748, 0.675911841219724, 0.745156001208767, 0.807166423246402, 0.861341129837251, 0.907154210124478, 0.944160925265473, 0.972002025815363, 0.990407239206624, 0.999197893469363, 0.998288651722103,
0.987688340595138, 0.967499864544408, 0.937919206887704, 0.899233527260826, 0.851818373962972, 0.796134038253055, 0.732721085988247, 0.662195109982227, 0.585240754025510, 0.502605066580843, 0.415090248673835, 0.323545866380009, 0.228860603507013, 0.131953634534243, 0.0337657015587562, -0.0647500181304328,
-0.162637165194890, -0.258945482279751, -0.352740038810489, -0.443110307002175, -0.529179000974195, -0.610110593163201, -0.685119425359456, -0.753477335627384, -0.814520727070512, -0.867657009819546, -0.912370353706714, -0.948226695781328, -0.974877954055122, -0.992065406571739, -0.999622202997347, -0.997474984350731,
-0.985644595148997, -0.964245881055618, -0.933486573995179, -0.893665275557789, -0.845168558269628, -0.788467212869540, -0.724111678021981, -0.652726696833165, -0.575005252043276, -0.491701838769995, -0.403625140109338, -0.311630176696852, -0.216610006438655, -0.119487054988758, -0.0212041611335505, 0.0772845759878599,
0.175023058975285, 0.271062473657499, 0.364470499879154, 0.454340362168866, 0.539799632427741, 0.620018699184368, 0.694218821199767, 0.761679687240114, 0.821746408629595, 0.873835876701791, 0.917442423433567, 0.952142730309724, 0.977599937764792, 0.993566915308769, 0.999888660591489, 0.996503804116233,
0.983445204995330, 0.960839631964316, 0.928906532751298, 0.887955903748049, 0.838385280663432, 0.780675879372910, 0.715387924427582, 0.643155210572020, 0.564678950066079, 0.480720965538817, 0.392096294456247, 0.299665276996257, 0.204325204139264, 0.107001607052153, 0.00863927232549537, -0.0898069297148414,
-0.187381314585722, -0.283136661128381, -0.376143406830793, -0.465498671725509, -0.550335023261759, -0.629828897065438, -0.703208591840560, -0.769761760800563, -0.828842326904764, -0.879876754769274, -0.922369618366427, -0.955908410463418, -0.980167547111590, -0.994911528312078, -0.999997224175046, -0.995375264379227,
-0.981090517443334, -0.957281655156723, -0.924179806397673, -0.882106313407595, -0.831469612302545, -0.772761268105050, -0.706551202786880, -0.633482162647440, -0.554263478736693, -0.469664180894493, -0.380505532252943, -0.287653056674601, -0.192008136521520, -0.0944992623213991, 0.00392698072380246, 0.102315101886539,
0.199709980514407, 0.295166138038260, 0.387756916378113, 0.476583473645635, 0.560783509815936, 0.639539637662464, 0.712087317692860, 0.777722280055610, 0.835807361368273, 0.885778690097328, 0.927151160444101, 0.959523141597856, 0.982580376640236, 0.996099033251688, 0.999947876604571, 0.994089543349219,
0.978580904325473, 0.953572512478617, 0.919307141339182, 0.876117428254551, 0.824422645251177, 0.764724628874819, 0.697602908520652, 0.623709080545813, 0.543760482778796, 0.458533230831479, 0.368854683815130, 0.275595412600709, 0.179660748593197, 0.0819819950616476, -0.0164926136573281, -0.114807117317581,
-0.212007109922050, -0.307149004793269, -0.399309194613317, -0.487593017510555, -0.571143442153087, -0.649149387536929, -0.720853596702917, -0.785559987947003, -0.842640412160429, -0.891540750701937, -0.931786294605665, -0.962986352905092, -0.984838045336978, -0.997129242606799, -0.999740625672616, -0.992646844056464,
-0.975916761938747, -0.949712789646630, -0.914289307026130, -0.869990194003309, -0.817245492307110, -0.756567230760720, -0.688544454668300, -0.613837507550104, -0.533171620737190, -0.447329873055838, -0.357145588946850, -0.263494248816364, -0.167284990149970, -0.0694517818945103, 0.0290556422159688, 0.127281003373913,
0.224270760949379, 0.319083369159829, 0.410798417297745, 0.498525564785646, 0.581413184319829, 0.658656629197908, 0.729506044573661, 0.793273646809804, 0.849340400263315, 0.897162026686948, 0.936274288909695, 0.966297497503909, 0.986940196689757, 0.998001993695415, 0.999375504106507, 0.991047394319906,
0.973098510982126, 0.945703096155735, 0.909127095832718, 0.863725578215192, 0.809939286825997, 0.748290361910482, 0.679377271664694, 0.603869002496122, 0.522498564715948, 0.436055876707622, 0.345380096649905, 0.251351476235615, 0.154882815467577, 0.0569106014859085, -0.0416140825518728, -0.139734790284324,
-0.236498997023725, -0.330967346563474, -0.422222770149962, -0.509379389094972, -0.591591114604805, -0.668059861341758, -0.738043294983249, -0.800862038567848, -0.855906267671136, -0.902641630387714, -0.940614434649876, -0.969456052526198, -0.988886498744503, -0.998717148700020, -0.998852569563171, -0.989291446711198,
-0.970126596490105, -0.941544065183020, -0.903821322931938, -0.857324570145646, -0.802505182542385, -0.739895329337642, -0.670102807114307, -0.593805139526380, -0.511743000114341, -0.424713022081554, -0.333560064831927, -0.239169012343003, -0.142456182993156, -0.0443604342336034, 0.0541659515417433, 0.152166511451484,
0.248689887164858, 0.342799060386402, 0.433580449132212, 0.520152776493846, 0.601675625794849, 0.677357599089101, 0.746463999800899, 0.808323964926108, 0.862336977557309, 0.907978696511290, 0.944806046466880, 0.972461519199504, 0.990676644157565, 0.999274594689349, 0.998171904620035, 0.987379278514827,
0.967001487762437, 0.937236353487686, 0.898372826166838, 0.850788180588052, 0.794944353387510, 0.731383458715156, 0.660722525562598, 0.583647507841499, 0.500906625360716, 0.413303100345869, 0.321687360012960, 0.226948780890808, 0.130007055036036, 0.0318032619544833, -0.0667092670999465, -0.164574203762531,
-0.260841506289903, -0.354576642263842, -0.444869660735319, -0.530844025739510, -0.611665125428735, -0.686548374219374, -0.754766829299691, -0.815658247559921, -0.868631514438194, -0.913172382273058, -0.948848462456602, -0.975313422925793, -0.992310350244224, -0.999674243636218, -0.997333616761983, -0.985311191684313,
-0.963723678290009, -0.932780641307352, -0.892782465918221, -0.844117441714098, -0.787257993303952, -0.722756094166077, -0.651237908264765, -0.573397711449273, -0.489991151644234, -0.401827913259485, -0.309763857030723, -0.214692711595228, -0.117537397457843, -0.0192410675716325, 0.0792420484915564, 0.176955907898970,
0.272951935517313, 0.366298232379078, 0.456088622261896, 0.541451448559776, 0.621558036048674, 0.695630735402642, 0.762950472366612, 0.822863728301042, 0.874788884333453, 0.918221867529835, 0.952741044274703, 0.978011313356413, 0.993787359023354, 0.999916032431425, 0.996337838364386, 0.983087512794541,
0.960293685676943, 0.928177632250618, 0.887051124968773, 0.837313406910770, 0.779447316057058, 0.714014598051266, 0.641650452951816, 0.563057368911361, 0.478998302644763, 0.390289272887461, 0.297791438744591, 0.202402739831719, 0.105049179362040, 0.00667583480114843, -0.0917623166451206, -0.189309668646243,
-0.285019262469973, -0.377961979757159, -0.467235562107885, -0.551973369919598, -0.631352795449380, -0.704603248428773, -0.771013636709533, -0.829939269320517, -0.880808114923002, -0.923126354909364, -0.956483177237392, -0.980554764463184, -0.995107437258145, -0.999999922893717, -0.995184726672197, -0.980708592990188,
-0.956712051558832, -0.923428053185976, -0.881179708363663, -0.830377150614053, -0.771513555043328, -0.705160350754312, -0.631961673594109, -0.552628113087742, -0.467929814260579, -0.378689001314870, -0.285771995738146, -0.190080806329236, -0.0925443727830621, 0.00589045216103947, 0.104268094465144, 0.201633535202275,
0.297041581577028, 0.389566042557161, 0.478308720042225, 0.562408128285632, 0.641047856924812, 0.713464496433956, 0.778955049061354, 0.836883753308413, 0.886688255700559, 0.927885069936238, 0.960074270418487, 0.982943374605696, 0.996270376492941, 0.999925901775817, 0.993874463775118, 0.978174807930265,
0.952979341517217, 0.918532654127013, 0.875169143267650, 0.823309768139218, 0.763457963095587, 0.696194750463496, 0.622173100162199, 0.542111590878786, 0.456787434334296, 0.367028930359057, 0.273707426020814, 0.177728856863960, 0.0800249523748130, -0.0184558089525184, -0.116757407144309, -0.213925561485702,
-0.309016994374950, -0.401108588363064, -0.489306347484964, -0.572754075888567, -0.650641689512372, -0.722213080124420, -0.786773455380986, -0.843696083650190, -0.892428378123718, -0.932497261154178, -0.963513756742748, -0.985176766594716, -0.997275993086157, -0.999693980766518, -0.992407256578852, -0.975486557728811,
-0.949096144990295, -0.913492208114000, -0.869020378818633, -0.816112375507883, -0.755281812285189, -0.687119212951036, -0.612286278385335, -0.531509462965285, -0.445572922376897, -0.355310901280419, -0.261599634727995, -0.165348841951888, -0.0674928950989053, 0.0310182513578315, 0.129228282475433, 0.226183806443067,
0.320943609807221, 0.412587794473066, 0.500226707783204, 0.583009578983345, 0.660132778234681, 0.730847617997353, 0.794467621051437, 0.850375184599931, 0.898027575760620, 0.936962200244736, 0.966801093075418, 0.987254587751762, 0.998124128239275, 0.999304196488835, 0.990783336772423, 0.972644266891692,
0.945063075179802, 0.908307511091783, 0.862734385977789, 0.808786109271773, 0.746986393721116, 0.677935171349503, 0.602302769507245, 0.520823403546116, 0.434288049289797, 0.343536764491561, 0.249450533820281, 0.152942716540872, 0.0549501799124448, -0.0435757956216944, -0.141678751162651, -0.238406334355361,
-0.332819544522994, -0.424001848187439, -0.511068076485486, -0.593173018107150, -0.669519624338816, -0.739366746559169, -0.802036331074781, -0.856920001450147, -0.903484964433038, -0.941279182142260, -0.969935760307973, -0.989176509964781, -0.998814648021921, -0.998756610494226, -0.989002960791592, -0.969648384249581,
-0.940880768954224, -0.902979381784135, -0.856312157376272, -0.801332126333271, -0.738573017346144, -0.668644075925549, -0.592224150039745, -0.510055100073975, -0.422934597085312, -0.331708379265198, -0.237262041781567, -0.140512439701961, -0.0423987874555876, 0.0561264587622677, 0.154106847132682, 0.250591215143780,
0.344642923174507, 0.435348947094810, 0.521828741614001, 0.603242788335117, 0.678800745532949, 0.747769120540683, 0.809478390263939, 0.863329500698409, 0.908799682356035, 0.945447525145274, 0.972917263440128, 0.990942229739981, 0.999347443393019, 0.998051309252864, 0.987066409778365, 0.966499382887074,
0.936549886748192, 0.897508661564435, 0.849754707158412, 0.793751603762675, 0.730043011729913, 0.659247393850829, 0.582052011513663, 0.499206252988763, 0.411514358605109, 0.319827613440464, 0.225036083316014, 0.128059974319935, 0.0298406997386746, -0.0686682588843781, -0.166510607845101, -0.262736524674396,
-0.356411878713249, -0.446627299356684, -0.532507003934973, -0.613217299533782, -0.687974676220444, -0.756053413109658, -0.816792623431512, -0.869602670210485, -0.913970890274060, -0.949466571024084, -0.975745131657964, -0.992551468249736, -0.999722430218001, -0.997188404139995, -0.984973989536599, -0.963197760067995,
-0.932071112458212, -0.891896214322823, -0.843063070821639, -0.786045738612394, -0.721397723859212, -0.649746608970392, -0.571787960227613, -0.488278575449232, -0.400029137237269, -0.307896343128008, -0.212774589044232, -0.115587286783459, -0.0172778998293627, 0.0811992154923688, 0.178888074602276, 0.274840345062014
};
float freq;
float ind;
float val = 0;
float maxV = 0;
int XN = sizeof(y) / sizeof(*y);
for (k = 0; k < XN; k++) {
x[k] = y[k];
}
struct timespec sT, eT;
clock_gettime(CLOCK_MONOTONIC, &sT);
printf("Starting DFT\n");
// Calculate DFT of x using brute force
for (k = 0; k < 2000; ++k) {
// Real part of X[k]
Xre[k] = 0;
for (n = 0; n < XN; ++n)
Xre[k] += x[n] * cos(n * k * PI2 / N);
// Imaginary part of X[k]
Xim[k] = 0;
for (n = 0; n < XN; ++n)
Xim[k] -= x[n] * sin(n * k * PI2 / N);
// Power at kth frequency bin
P[k] = Xre[k] * Xre[k] + Xim[k] * Xim[k];
// printf("\n%+f %+f %f",Xre[k],Xim[k],P[k]);
}
printf("DFT over\n");
ind = 0;
#if 1
printf("Freq Compute start\n");
for (k = 0; k < 2000; k++) {
//val = (Xre[k] * Xre[k]) + (Xim[k] * Xim[k]);
val = P[k];
if (val > maxV) {
//printf("%f\n", val);
maxV = val;
ind = k;
a = val;
}
}
printf("Freq Compute over\n");
#endif
freq = ind * 0.195;
printf("maxV=%f ind = %f Freq= %f\n", maxV, ind, freq);
clock_gettime(CLOCK_MONOTONIC, &eT);
printf("%ld.%09ld %ld uSec\n",
eT.tv_sec, eT.tv_nsec,
((eT.tv_sec - sT.tv_sec) * 1000000 + (eT.tv_nsec - sT.tv_nsec) / 1000));
return 0;
}