diff --git a/Cargo.toml b/Cargo.toml index b11ff6d..5132f3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,9 @@ authors = ["Ivan Ukhov "] license = "MIT" repository = "https://github.com/hilbert-space/fft" homepage = "https://github.com/hilbert-space/fft" -description = "FFT." +description = """ +The package provides an algorithm to compute the discrete Fourier transform and +its inverse.""" + +[dev-dependencies] +assert = "*" diff --git a/README.md b/README.md index 95169d7..0a901a2 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ # FFT + +The package provides an [algorithm][1] to compute the [discrete Fourier +transform][2] and its inverse. + +## Contributing + +1. Fork the project. +2. Implement your idea. +3. Open a pull request. + +[1]: https://en.wikipedia.org/wiki/Fast_Fourier_transform +[2]: https://en.wikipedia.org/wiki/Discrete_Fourier_transform diff --git a/src/lib.rs b/src/lib.rs index e69de29..208ba55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,11 @@ +pub enum Direction { + Forward, + Backward, +} + +pub fn transform(data: &mut [f64], _: Direction) { + let n = data.len(); + if n < 2 || n & (n - 1) != 0 { + panic!("the data size should be a power of two"); + } +} diff --git a/tests/fixtures.rs b/tests/fixtures.rs new file mode 100644 index 0000000..43a464a --- /dev/null +++ b/tests/fixtures.rs @@ -0,0 +1,261 @@ +pub const TIME_DATA: [f64; 256] = [ + 0.242578298897752, 0.013469574513598, + 0.383138850044058, 0.414652690484492, + 0.067768972864267, 0.993126929734427, + 0.484308046514312, 0.765337766038877, + 0.031833815403205, 0.030935481670748, + 0.932640440265015, 0.887879534106646, + 0.591329730391190, 0.478778684734729, + 0.833354336597656, 0.186335196805343, + 0.735652707394051, 0.115053171811184, + 0.698658630577223, 0.355604111382553, + 0.638300006575091, 0.908210507551306, + 0.294000414802693, 0.264971588861650, + 0.377493997745912, 0.541620115536088, + 0.009281815034003, 0.999465276486923, + 0.012901915708977, 0.842497320772380, + 0.852470221394892, 0.467010983949067, + 0.053607231962312, 0.976747590571990, + 0.196754743436703, 0.856972940665191, + 0.144213759873162, 0.800662188232253, + 0.729397619482781, 0.985790647094041, + 0.183405709538332, 0.499760210746788, + 0.469862021258968, 0.970991299474142, + 0.450770261907377, 0.095791877292000, + 0.974081646638960, 0.390235061007661, + 0.680670355763598, 0.026669318800172, + 0.231241074498389, 0.468739094430925, + 0.097960100554843, 0.415410025238716, + 0.796294187100741, 0.316402602156812, + 0.778534449533808, 0.828493314715332, + 0.487140420585470, 0.369048780002188, + 0.602845496778770, 0.024264360789333, + 0.811111786314804, 0.355792592910953, + 0.806109054389460, 0.274877123662679, + 0.859817398646761, 0.951019056118568, + 0.777276184771804, 0.680837459713611, + 0.835185406652831, 0.961129614133914, + 0.705424748689600, 0.073751226101886, + 0.536857094400030, 0.957185581306548, + 0.418065019146570, 0.418776796394389, + 0.381617000504218, 0.836927474400460, + 0.240062248539209, 0.726211198478104, + 0.431612821496843, 0.116690897437134, + 0.223913225915243, 0.309587957481662, + 0.244801394289733, 0.377033827536290, + 0.807539402417624, 0.314736433008097, + 0.775229567091553, 0.283334107735816, + 0.996348715851246, 0.632867311887847, + 0.600910899043507, 0.509480224228222, + 0.834128603727617, 0.199442850053051, + 0.035980841627336, 0.730005230628888, + 0.197911179716657, 0.293197497862017, + 0.770346566927781, 0.214750355209573, + 0.309220007299082, 0.060662675677176, + 0.557590106296162, 0.416916519597600, + 0.115944876855214, 0.685545305575032, + 0.959950799569465, 0.893088363992557, + 0.136133622907164, 0.997800200710911, + 0.027973348287853, 0.148064673947200, + 0.522975030598685, 0.641339272093651, + 0.989146078000379, 0.578132952367017, + 0.680530432462939, 0.674978404620187, + 0.362046451476424, 0.914709964261721, + 0.530369346742690, 0.917610704394807, + 0.283108763528573, 0.208988624722226, + 0.471815706450406, 0.806578311979109, + 0.161689432878834, 0.514298394561884, + 0.813117401587366, 0.064168478857804, + 0.479624163117085, 0.043309508843026, + 0.902915124736221, 0.294501441668021, + 0.685730114432857, 0.066033273034745, + 0.821219894951777, 0.242774454524170, + 0.310257187723302, 0.492554065535103, + 0.356179448476145, 0.307990538565438, + 0.396981669309075, 0.070916077620776, + 0.886516572389061, 0.684032142946512, + 0.528226502020018, 0.902819450433748, + 0.686503439995695, 0.063316007639894, + 0.152140403702920, 0.023765034984688, + 0.418942987648278, 0.174793404608403, + 0.752751253430150, 0.490316400532758, + 0.747743754064545, 0.329274562806485, + 0.117577088585858, 0.118127862512194, + 0.374985242437099, 0.376969640318756, + 0.728744837329138, 0.014480990830102, + 0.382012881516485, 0.490499647562625, + 0.827576585033711, 0.079664661586128, + 0.923967278061419, 0.118042378275675, + 0.938251679268783, 0.195973470432671, + 0.726117561909425, 0.857863011703763, + 0.103637705139647, 0.838910282048821, + 0.565110394528653, 0.810400843066350, + 0.406969416144755, 0.934977144903958, + 0.160874400828441, 0.816054723605539, + 0.431739638294903, 0.248100822441327, + 0.830522771380154, 0.596218586245653, + 0.645779030698249, 0.608168945465316, + 0.495466435558846, 0.304382437516182, + 0.755627334469756, 0.828610433185757, + 0.455550553023606, 0.438144667743773, + 0.897430769585739, 0.118944427519545, + 0.098993320995473, 0.780745970914488, + 0.997533159795000, 0.539816674562086, + 0.698849364974932, 0.561277133674024, + 0.384785659324744, 0.092576270966128, + 0.929386127707263, 0.192648375962231, + 0.841254797224540, 0.969376952838794, + 0.318446361608080, 0.127999546997249, + 0.288386382762523, 0.909935089717589, + 0.279052883516556, 0.041813262757758, + 0.755507169643188, 0.809000193052460, + 0.866244632688465, 0.973541595029431, + 0.313587659650290, 0.467795742427835, + 0.243042984624879, 0.823442590340712, + 0.599615856353015, 0.743697725117066, + 0.327666042525166, 0.083176720460493, + 0.951140779513466, 0.823081282816399, + 0.527120295226164, 0.310801866143384, + 0.646964271854127, 0.528517052311691, + 0.786098202590876, 0.952490944858869, + 0.515310243012062, 0.819254303732540, + 0.207082832794209, 0.441170772277364, + 0.757169665655666, 0.750570674776365, + 0.841330966372663, 0.249551825341560, + 0.217528515596655, 0.001761632972286, + 0.607765365209321, 0.712493073061338, + 0.871078941911030, 0.223776698682353, + 0.014974754310667, 0.680695699379172, + 0.452619465744411, 0.175360766321123, +]; + +pub const FREQUENCY_DATA: [f64; 256] = [ + 67.506562667203411, 62.798747687506840, + -2.916435892389177, 2.258137395096745, + -1.213411748796466, -2.824305441697990, + -3.884636396852776, -6.613971933163763, + 5.049316157478026, 0.456883084114653, + -0.466361441955149, -5.481824322589378, + 0.803910676769211, 0.147054274059631, + -6.749332581521054, 4.840407656923564, + -0.262756329494097, 3.254410744830774, + -6.973431206318843, 1.575164194822261, + -5.636666719031166, -3.624154259337717, + -1.286794839414061, -3.596629452384591, + -1.949442402686498, -1.250892366300883, + -2.956509867164284, -3.563268353779529, + -1.629751417352084, 4.497170521285449, + 1.320095075374985, -2.156073489574812, + -2.002497059929997, -3.607915605047517, + 2.554697843376545, -2.629716248632329, + 2.398999344465010, -0.074112453018754, + 1.495969367959889, 1.905315704750475, + -2.236600417683027, 0.738926830782050, + -0.048604481554162, -2.372949299902079, + 3.012458946272938, -6.457540983540477, + -0.751768100557872, 2.124100304630884, + -1.792775263985138, 0.264154836959835, + -0.307176194080528, -5.087687483603743, + -1.005151880556302, 2.902422614798144, + 0.724987856799877, -2.068196780758698, + -0.342018034885117, 2.522012423589103, + -0.434808099354017, 2.147158479497558, + -2.674474141084141, -2.563372692380105, + -6.632075418392929, -1.685124294109070, + -2.699185197567189, -5.897826029871508, + -0.241335159541328, 3.101405290332420, + 2.979104548159058, -1.160028660033103, + 4.840508528923102, -3.204368559739159, + -3.399157829092539, 2.161358937350348, + 4.878539287722978, -1.459376706664861, + -0.426496319168839, 3.030254700260016, + 5.146915245537944, 0.282061559224328, + -0.008414044119338, -3.991754204800235, + 3.638877999600389, 1.258926814764483, + 1.909130437842078, 0.995539872732337, + 6.414605960628886, -5.679739832207080, + 3.905872946117078, -2.826501461749101, + 0.967386492473095, -5.576552424605286, + -2.853890281653033, -3.715099310600197, + -0.998730499887771, -1.368332585830762, + 0.539665253171003, 0.702327760413163, + -2.202133267022090, 1.329217526808806, + 0.923776499928182, 2.150735454872208, + 2.473335187775022, -0.373719659978438, + -2.253868990511847, -2.913770876062729, + -0.368246167526135, -1.498059975229275, + -0.075789241425793, 1.719766675858378, + -6.249259838595410, 6.001167756824215, + -4.027346047616200, 5.509694798065765, + -3.923350081315725, -0.896137928694981, + -0.923595644189471, -1.735427210626094, + -3.429327683732347, 0.363408609675781, + -2.276214983007463, -0.860532742279556, + 2.965000493451818, 5.042549679265357, + 2.949752700932401, 2.757985604650260, + -7.551126038911468, -1.253320876553430, + -3.991693675979835, -2.395612192990079, + -3.231893261005089, -3.499165263231712, + -1.385547239860866, 4.278296734710755, + -2.521465228517418, -1.483012583302978, + 2.166953458666462, 3.698992188846230, + -1.718586561803937, -2.848378813769423, + 2.356251252889540, 3.100152502890587, + -1.680667450021412, -5.672119730229592, + 3.266763808523838, -4.939142356746459, + -0.677140885336755, 7.689403378304775, + 0.714397029389201, -1.027012942409025, + 4.548911919541158, -3.110338786263284, + 0.963824257168846, -1.315676780411989, + -0.630783224843193, -1.040038800556799, + 0.051441799174254, 8.187374675121179, + 2.897552781182147, 4.424347873913396, + 3.813912169625594, 1.672124644400444, + -1.849780677085411, -6.075332676660434, + -3.451402723746721, -0.201945959802337, + -2.768891360840025, 1.367687274872978, + -1.559401743047184, -2.563598701770972, + -1.874383165372955, -1.816379942063397, + 4.841738890983525, 4.304535776881384, + -1.617862103749635, 4.505029568623651, + 3.144799926493496, 0.532041249412581, + -3.772195284223951, 2.554683615488583, + 1.466063738740647, 0.328554468964208, + 2.211012545853337, -3.584550392170861, + -5.848019862181538, -1.853688693742572, + -1.310659972997355, -4.744849190522960, + 2.553617632271563, -2.515247887047404, + 4.482138368790012, -8.618372361868635, + 0.884305207470568, 1.209832476084043, + 0.983852564385845, -1.970780039723666, + -4.755886984684762, -2.371006165743844, + -2.386952673947499, -0.380046032794893, + 8.202058412989162, -1.912136722291743, + 1.303130356713501, 0.110408680312637, + 4.396893371992936, -1.781029123711262, + -5.653027779602041, -4.457764259327377, + 2.344622479665089, 0.978245756031540, + 0.085165407317421, 1.846582481253430, + 2.305750958010686, 0.037116571959843, + 2.090636515202623, -2.544058986903603, + -1.098781173708802, -4.976387399730235, + 3.116401263119525, 0.806086033998370, + -7.921745368568139, -7.472969089954924, + 1.411453345272358, -1.449094611132407, + -0.804853562658514, 4.667294297528407, + 4.288737227223235, -3.968319162458097, + -2.972572998085656, 5.831133886072563, + 0.840509623087476, 4.498605610210712, + 2.983076467939862, -3.013246639835036, + 3.880468387697881, 1.634838899334426, + -4.944656692463916, 5.315462183419951, + 4.133604334208203, -3.445364011535503, + -3.599237215989622, -1.105183198676706, + -3.458371226842255, 8.013344593032475, + -5.278077778941197, -4.066322943629892, + 0.772765865031132, -8.707366039824819, + -0.108445528881629, -0.965343418118287, + 4.308501796277244, -0.278006225941031, + -5.303978155417413, -0.288754984960703, + 0.077148388378536, 5.819389938527753, +]; diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..40dd210 --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1,18 @@ +extern crate assert; +extern crate fft; + +mod fixtures; + +#[test] +fn transform_forward() { + let mut data = fixtures::TIME_DATA.to_vec(); + fft::transform(&mut data, fft::Direction::Forward); + assert::close(&data, &fixtures::FREQUENCY_DATA[..], 1e-13); +} + +#[test] +fn transform_inverse() { + let mut data = fixtures::FREQUENCY_DATA.to_vec(); + fft::transform(&mut data, fft::Direction::Inverse); + assert::close(&data, &fixtures::TIME_DATA[..], 1e-13); +}