Что можно улучшить в коде?
С помощью FFT определены амплитуда, частота и начальная фаза 512 гармоник. Код преобразования аудио файла с помощью FFT, Program.cs:
using NAudio.Wave;
using System;
using System.IO;
namespace fft
{
class Program
{
static void Main(string[] args)
{
using (var ms = File.OpenRead(@"d:\source.wav"))
{
using (WaveFileReader rdr = new WaveFileReader(ms))
{
byte[] buffer = new byte[4096]; //4096 buffer size
int bytesRead;
int total = 0;
int count = 0;
SampleAggregator sampleAggregator = new SampleAggregator();
sampleAggregator.PerformFFT = true;
sampleAggregator.FftCalculated += new EventHandler<FftEventArgs>(FftCalculated);
do
{
bytesRead = rdr.Read(buffer, 0, buffer.Length);
count++;
for (int startIndex = 0; startIndex < bytesRead - 4; startIndex += 4)
sampleAggregator.Add(BitConverter.ToInt32(buffer, startIndex));
total += bytesRead;
} while (bytesRead > 0);
sampleAggregator.PerformFFT = false;
}
}
Console.ReadKey();
}
static void FftCalculated(object sender, FftEventArgs e)
{
int n = e.Result.Length/2;
float[] X_array = new float[n]; // X real
float[] Y_array = new float[n]; // Y imaginary
float[] A_array = new float[n]; // magnitude
float[] phi_proc = new float[n]; // new phi
float[] frec = new float[n];
for (var i = 0; i < n; i++)
{
float X = e.Result[i].X;
float Y = e.Result[i].Y;
X_array[i] = X;
Y_array[i] = Y;
// Magnitude = sqrt(X^2 + Y^2)
float A = (float)Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));
A_array[i] = A;
// Phase = ArcTan(Y/X)
float phi_ = Convert.ToSingle(Math.Atan2(Y, X));
phi_proc[i] = phi_;
float F = i * 44100f / (2 * n);
frec[i] = F;
}
StreamWriter sr = new StreamWriter(@"d:\out.txt");
sr.Write("{0, 15}{1, 15}{2, 15}", "Амплитуда", "Фаза", "Частота\n");
for (int i = 0; i < n; i++)
{
sr.Write("{0, 15}\t{1, 15}\t{2, 15}\n", A_array[i], phi_proc[i], frec[i]);
}
sr.Close();
}
}
}
SampleAggregator.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using NAudio.Dsp;
namespace fft
{
public class SampleAggregator
{
// volume
public event EventHandler<MaxSampleEventArgs> MaximumCalculated;
private float maxValue;
private float minValue;
public int NotificationCount { get; set; }
int count;
// FFT
public event EventHandler<FftEventArgs> FftCalculated;
public bool PerformFFT { get; set; }
private Complex[] fftBuffer;
private FftEventArgs fftArgs;
private int fftPos;
private int fftLength;
private int m;
public SampleAggregator(int fftLength = 1024)
{
if (!IsPowerOfTwo(fftLength))
{
throw new ArgumentException("FFT Length must be a power of two");
}
this.m = (int)Math.Log(fftLength, 2.0);
this.fftLength = fftLength;
this.fftBuffer = new Complex[fftLength];
this.fftArgs = new FftEventArgs(fftBuffer);
}
bool IsPowerOfTwo(int x)
{
return (x & (x - 1)) == 0;
}
public void Reset()
{
count = 0;
maxValue = minValue = 0;
}
public void Add(float value)
{
if (PerformFFT && FftCalculated != null)
{
fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftBuffer.Length));
fftBuffer[fftPos].Y = 0;
fftPos++;
if (fftPos >= fftBuffer.Length)
{
fftPos = 0;
// 1024 = 2^10
FastFourierTransform.FFT(true, m, fftBuffer);
FftCalculated(this, fftArgs);
}
}
maxValue = Math.Max(maxValue, value);
minValue = Math.Min(minValue, value);
count++;
if (count >= NotificationCount && NotificationCount > 0)
{
if (MaximumCalculated != null)
{
MaximumCalculated(this, new MaxSampleEventArgs(minValue, maxValue));
}
Reset();
}
}
}
public class MaxSampleEventArgs : EventArgs
{
[DebuggerStepThrough]
public MaxSampleEventArgs(float minValue, float maxValue)
{
this.MaxSample = maxValue;
this.MinSample = minValue;
}
public float MaxSample { get; private set; }
public float MinSample { get; private set; }
}
public class FftEventArgs : EventArgs
{
[DebuggerStepThrough]
public FftEventArgs(Complex[] result)
{
this.Result = result;
}
public Complex[] Result { get; private set; }
}
}
Выводит следующий результат:
Амплитуда Фаза Частота
1,37635E+08 3,141593 0
9,872906E+07 -0,3174495 43,06641
2,693165E+07 3,054966 86,13281
4,057771E+07 -2,695019 129,1992
3,775276E+07 0,1797561 172,2656
8874377 2,548879 215,332
3,056839E+07 -2,072765 258,3984
3,477777E+07 0,4656045 301,4648
1,996756E+07 -2,944435 344,5313
9527217 -0,2522248 387,5977
3,09956E+07 2,621248 430,6641
2,766847E+07 -0,1084108 473,7305
3,245723E+07 -1,624003 516,7969
8221946 1,382699 559,8633
3,13396E+07 3,106157 602,9297
4,747455E+07 -0,1934181 645,9961
3,064184E+07 -2,467533 689,0625
1,632683E+07 1,046181 732,1289
9580996 1,830062 775,1953
1,733721E+07 -1,386621 818,2617
2759044 -2,58237 861,3281
2500503 1,662177 904,3945
6722505 -1,059276 947,4609
3197823 -2,659224 990,5273
4304571 0,6496741 1033,594
1627016 -2,234364 1076,66
4146548 -0,8395553 1119,727
3684058 -3,047166 1162,793
3232784 0,6181203 1205,859
4427241 -1,8927 1248,926
4181582 0,6159479 1291,992
5726374 -2,39327 1335,059
1504541 2,070046 1378,125
2946159 -1,343505 1421,191
5200244 0,6900653 1464,258
4010569 -2,121195 1507,324
1955903 -1,596277 1550,391
3920184 2,484337 1593,457
6433642 -0,5926874 1636,523
2583524 2,366535 1679,59
1773965 -2,307449 1722,656
5931704 -0,7929059 1765,723
8772014 2,176744 1808,789
6246009 -1,262712 1851,855
4309498 0,7958669 1894,922
6315455 -3,030753 1937,988
7998884 -0,8917868 1981,055
2486457 1,388703 2024,121
4185494 -2,464547 2067,188
1748211 1,871544 2110,254
1856945 0,2547571 2153,32
1031907 -1,658766 2196,387
1266610 -2,95061 2239,453
2289228 -0,05332185 2282,52
3114942 -2,808688 2325,586
2220020 -0,03317619 2368,652
2220844 -2,469988 2411,719
1686113 -0,7643628 2454,785
1047378 -2,660212 2497,852
615958,3 1,436106 2540,918
1403766 -1,104017 2583,984
976041,3 2,601465 2627,051
2734417 -0,4571997 2670,117
2911894 3,008721 2713,184
2621899 -1,100849 2756,25
982119,1 0,8500024 2799,316
1847694 2,514631 2842,383
4606243 -0,7821463 2885,449
3787969 2,917466 2928,516
8877559 0,2767128 2971,582
3,619529E+07 -2,545841 3014,648
3,148596E+07 0,598125 3057,715
3746493 -2,472877 3100,781
830009,3 0,5579143 3143,848
454884,2 2,318099 3186,914
1921721 -2,818796 3229,98
2720334 -0,06990055 3273,047
773526,5 2,716008 3316,113
991095,9 -0,3717431 3359,18
370259,1 2,489391 3402,246
816891,8 -0,3727157 3445,313
603778,2 1,898032 3488,379
1243691 -2,082001 3531,445
1074989 -0,2578799 3574,512
66772,98 -0,05730519 3617,578
1292134 -2,337628 3660,645
1310137 -0,2033752 3703,711
618416,1 1,38453 3746,777
638616,1 -2,753081 3789,844
1184789 -0,7677395 3832,91
615659 2,689984 3875,977
1107154 2,441525 3919,043
1576492 -0,5585968 3962,109
652733,1 -0,7071712 4005,176
948865,8 1,762177 4048,242
1023113 -0,7465729 4091,309
1034635 -2,611075 4134,375
1107797 1,2744 4177,441
1519462 -1,141 4220,508
569109,1 2,644734 4263,574
588931,6 -0,8147474 4306,641
313872,9 -1,652717 4349,707
1315602 1,453712 4392,773
1382194 -1,646547 4435,84
397498,1 -2,74053 4478,906
2568746 -0,1058861 4521,973
3092902 -3,039691 4565,039
1024402 -0,05963277 4608,105
707777,4 -0,4127465 4651,172
195717,4 2,92164 4694,238
648599,6 -3,063361 4737,305
671911,4 0,286155 4780,371
953865,7 -0,8912678 4823,438
650526 0,8622005 4866,504
1186269 -2,696839 4909,57
1078016 0,4043865 4952,637
1012172 -2,171409 4995,703
712915,9 0,4397762 5038,77
1088657 -1,958312 5081,836
963623,9 1,678041 5124,902
675288,7 -1,699725 5167,969
821716 -0,6037893 5211,035
193930,5 0,9480741 5254,102
770689,4 -1,730797 5297,168
668285,1 1,937282 5340,234
873034,6 -1,132364 5383,301
346136,1 1,362874 5426,367
192686 -1,601493 5469,434
444887,6 -0,337891 5512,5
36712,4 -0,6167735 5555,566
171707,9 -1,72992 5598,633
491799,8 -0,1735717 5641,699
162790,2 1,71206 5684,766
774867,9 -1,88205 5727,832
321691,2 -3,092386 5770,898
...................................................................
407089,8 0,2651654 8742,48
355678,1 -2,107224 8785,547
310083,2 1,086776 8828,613
164458,8 -1,291515 8871,68
129096,7 -0,7981789 8914,746
577172,1 -1,489183 8957,813
535018,8 1,185401 9000,879
634055,3 -1,744481 9043,945
507648,6 1,569295 9087,012
278733,8 -1,209023 9130,078
362415,5 0,3832399 9173,145
532419,3 -2,014296 9216,211
401044,8 0,4671175 9259,277
385854,8 -1,755475 9302,344
122248,9 -1,265652 9345,41
48546,2 0,6723771 9388,477
185850,8 -0,2805638 9431,543
318726,8 -2,021466 9474,609
120382 0,7419412 9517,676
279961,8 -0,8201453 9560,742
291825,1 -0,6727744 9603,809
560844,9 2,43568 9646,875
546299,2 -0,9895962 9689,941
131387,7 0,9339966 9733,008
391302 -0,7179333 9776,074
21409,18 -2,997437 9819,141
90142,8 -0,8544825 9862,207
391029 -1,984326 9905,273
293622,8 0,3757861 9948,34
369789,4 -0,748113 9991,406
463953,5 2,328602 10034,47
310653,1 -0,7894809 10077,54
190959,5 -0,2650703 10120,61
47492,66 -1,903389 10163,67
235372,1 -1,175213 10206,74
234728,5 0,4564956 10249,8
455498,2 -2,258151 10292,87
449732,8 0,799491 10335,94
203316,5 -1,324437 10379
85121,31 -0,4261258 10422,07
273546,6 -1,375018 10465,14
175015 3,079525 10508,2
121348,3 0,2721767 10551,27
431120,8 -0,3579329 10594,34
347832,1 2,2752 10637,4
655489,1 -1,457085 10680,47
208940,7 1,545835 10723,54
110208,8 -1,210837 10766,6
136959,6 2,334763 10809,67
646642,6 -0,4615363 10852,73
275528,1 -2,956884 10895,8
264023,6 0,4015069 10938,87
257054,7 -1,389444 10981,93
96765,98 2,173659 11025
89608,51 -0,9954699 11068,07
366929,9 -1,535273 11111,13
467821,4 0,9709223 11154,2
192946 2,894901 11197,27
486996,1 -1,225242 11240,33
249776,6 0,04803916 11283,4
199946,5 -2,763811 11326,46
290771,2 -0,3811239 11369,53
254724,4 3,006238 11412,6
388126 -0,3031629 11455,66
201632 -3,046822 11498,73
467290,1 -0,585902 11541,8
424830,9 2,134953 11584,86
463910,3 -1,185225 11627,93
186633,4 2,299737 11671
265882,6 -1,058136 11714,06
540958,3 0,8847211 11757,13
348871,1 -1,873847 11800,2
42848,82 -2,989112 11843,26
228773,1 -0,8439134 11886,33
116892,1 0,05488167 11929,39
127908 2,345144 11972,46
499440,8 -1,339604 12015,53
436530,1 1,239592 12058,59
281511,6 -1,388246 12101,66
128884,6 1,696337 12144,73
285539,3 -0,7990029 12187,79
215221,6 2,41503 12230,86
212511,2 -0,8346258 12273,93
286025,1 -0,2285509 12316,99
173265,6 2,810702 12360,06
179124,4 0,1145218 12403,13
181786,8 -1,939869 12446,19
399855,4 -0,003901795 12489,26
275812,7 -2,950712 12532,32
99744,02 0,967459 12575,39
77759,06 0,2980953 12618,46
439379,4 -0,9125478 12661,52
327063,8 2,155473 12704,59
303660,3 -0,432981 12747,66
439110,2 -2,682404 12790,72
375834,6 0,5313507 12833,79
692050,9 -1,144768 12876,86
899157,5 2,037639 12919,92
553065,6 -1,218552 12962,99
387296,2 -0,1003001 13006,05
273863,8 -1,938238 13049,12
301825,4 1,427282 13092,19
265484,4 -1,779614 13135,25
137390,9 0,2286138 13178,32
338550,3 -0,3008576 13221,39
320558,2 2,995403 13264,45
492184,9 -0,912329 13307,52
314383,6 1,254047 13350,59
233947,1 -1,300414 13393,65
52128,86 -0,2290096 13436,72
189073 -0,05959874 13479,79
43716,71 3,106588 13522,85
218084,8 -0,8031774 13565,92
254938 2,787258 13608,98
422808,8 -0,6583306 13652,05
133323,6 2,193803 13695,12
262140,5 -0,1007498 13738,18
201085,6 2,236696 13781,25
526387 -1,117748 13824,32
127898,9 3,004457 13867,38
75073 1,217288 13910,45
234179,2 -0,5897456 13953,52
205216,3 0,9480705 13996,58
154677,2 -1,163525 14039,65
101789,9 -2,19061 14082,71
70660,41 -0,9976465 14125,78
160737,7 -0,7375044 14168,85
33656,25 0,6239181 14211,91
72595,66 0,2986096 14254,98
240884,5 -2,028966 14298,05
332211,1 0,4460075 14341,11
170656 -1,950373 14384,18
58296,8 0,1520191 14427,25
215829,9 -0,2801605 14470,31
142457,4 -2,538587 14513,38
171582,1 0,356312 14556,45
237834,8 -1,400423 14599,51
107239,4 2,43935 14642,58
239289,1 0,3403818 14685,64
129985,9 -1,067518 14728,71
222832,1 -0,4511884 14771,78
107144,2 -2,367797 14814,84
74223,79 1,468154 14857,91
202884,6 -0,7028238 14900,98
94931,53 -0,2352982 14944,04
24525,81 -1,664078 14987,11
247106,7 1,680805 15030,18
489472,3 -0,9126853 15073,24
242411,5 2,397371 15116,31
214493,6 -0,8062004 15159,38
14358,52 -2,970088 15202,44
228372,2 0,07976476 15245,51
237966,5 -2,525232 15288,57
238895 0,5377389 15331,64
227006,8 -1,018389 15374,71
62815,55 -1,915184 15417,77
59146,21 0,3909755 15460,84
217516,4 0,1088751 15503,91
48396,8 3,085744 15546,97
19491,73 -1,106534 15590,04
154407,6 -1,227129 15633,11
197445,2 -0,6012994 15676,17
208898 1,682533 15719,24
407386,4 -1,002899 15762,3
63866 -3,118124 15805,37
206151 1,561113 15848,44
279238,6 -1,03722 15891,5
20755,63 1,659211 15934,57
207772,9 0,06274817 15977,64
67124,13 2,505021 16020,7
180185,4 -1,251169 16063,77
254831,6 0,3275275 16106,84
193584,7 -2,579321 16149,9
31936,7 2,996479 16192,97
323402,2 -0,5959395 16236,04
271104,1 1,112661 16279,1
292882 -1,481264 16322,17
168304,4 1,02441 16365,23
55663,51 -1,793853 16408,3
85871,41 -0,4980028 16451,37
172433,2 1,411605 16494,43
350143,5 -1,182185 16537,5
48604,67 -0,1564384 16580,57
34959,98 0,2479544 16623,63
156579,3 -0,5921221 16666,7
116151,3 -2,662918 16709,77
258779,3 0,4845772 16752,83
45198,96 -1,359202 16795,9
70406,92 -0,6723601 16838,96
66774,18 -0,8966588 16882,03
128428,4 -0,617744 16925,1
140979,3 1,587383 16968,16
280834 -1,128473 17011,23
102337 -2,502975 17054,3
283287,5 0,3265435 17097,36
151975,3 -2,643661 17140,43
201590,2 -0,3267642 17183,5
118359,8 0,9091799 17226,56
179498,1 -1,415487 17269,63
202394,2 1,396686 17312,7
231037,3 -0,7043558 17355,76
41419,77 -2,926986 17398,83
185915 -1,523513 17441,89
126872,6 1,906348 17484,96
219416,2 -0,7838139 17528,03
183797,3 -0,3128181 17571,09
42174,49 2,389735 17614,16
169301,4 -0,2982703 17657,23
37117,43 2,984656 17700,29
97238,13 1,459621 17743,36
82646,74 -0,4535268 17786,43
71865,59 -0,008223356 17829,49
111838,8 2,243133 17872,56
90411,02 -1,12074 17915,63
278247,2 -1,015702 17958,69
154384,5 -2,692111 18001,76
135247,4 -0,03473107 18044,82
343375,9 -0,7261079 18087,89
269568,2 0,1009461 18130,96
1064260 -0,4961446 18174,02
4318394 2,557522 18217,09
3817022 -0,4996571 18260,16
253924,6 1,32888 18303,22
155257,8 2,030647 18346,29
200382,8 1,432151 18389,36
134515 0,9798414 18432,42
87995,19 -0,4506082 18475,49
68341,77 1,571618 18518,55
34704,02 1,545008 18561,62
217592,7 -1,146028 18604,69
45598,66 1,715822 18647,75
137021,3 -0,2546101 18690,82
36449,06 0,0425174 18733,89
184063,4 -0,4431018 18776,95
154889,7 0,7162107 18820,02
171592 0,02029408 18863,09
191761,6 -2,868127 18906,15
137085,2 -0,1173551 18949,22
218939 0,8032717 18992,29
202394,6 -2,117162 19035,35
232593,6 0,2800354 19078,42
100331,3 -2,235241 19121,48
277506,9 0,85051 19164,55
224497,2 -1,515977 19207,62
133286,8 0,4950542 19250,68
62575,92 -1,893883 19293,75
307186,1 0,576763 19336,82
196983,4 -1,480692 19379,88
171627,4 1,195461 19422,95
97067,67 -2,564923 19466,02
203645,6 -0,1813843 19509,08
32216,71 -0,3762972 19552,15
174123 -1,284576 19595,21
211578,5 0,7129873 19638,28
47298,41 -1,632651 19681,35
204052,8 -0,1317645 19724,41
99191,77 -1,610497 19767,48
53945,24 1,640093 19810,55
134271,2 1,032663 19853,61
163449,4 -0,2928131 19896,68
79200,4 -0,1318209 19939,75
86771,17 -3,107662 19982,81
181193,2 -0,4431666 20025,88
100615,4 -0,2202356 20068,95
69420,39 3,128529 20112,01
231349,8 0,04048931 20155,08
92054,8 1,654247 20198,14
47710,43 -0,8525124 20241,21
156213,2 -0,6774409 20284,28
161189,2 0,9777452 20327,34
115269,6 -1,923447 20370,41
148665,8 0,5388893 20413,48
117696,5 -1,574587 20456,54
92606,59 0,300347 20499,61
145723,1 0,3891755 20542,68
112504,9 -0,8060678 20585,74
73474,25 -2,336761 20628,81
256318,7 0,23377 20671,88
24250,88 -2,250594 20714,94
160476,2 -0,4384546 20758,01
15034,2 1,247216 20801,07
139379,5 0,8427607 20844,14
114546,1 -1,546166 20887,21
81208,93 0,320226 20930,27
43770,13 -1,4137 20973,34
161747 0,20294 21016,41
124452,9 -2,202655 21059,47
208195,3 0,4855228 21102,54
92038,16 -1,055747 21145,61
129751,5 0,381626 21188,67
38776,03 0,06692123 21231,74
103451 0,4821521 21274,8
77022,04 -0,9981962 21317,87
125995 -0,1490869 21360,94
57013,56 1,601962 21404
50614,99 -0,6230396 21447,07
94630,76 -0,1863516 21490,14
91186,84 0,1544781 21533,2
99161,91 -0,3041609 21576,27
46391,44 0,3795729 21619,34
91104,3 -0,05825521 21662,4
62201,8 0,5848758 21705,47
77783,59 -0,2326978 21748,54
54642,25 0,7926224 21791,6
93077,99 -0,4236717 21834,67
47430,64 0,09272207 21877,73
104056,9 -0,2599141 21920,8
30258,8 0,235479 21963,87
92160,1 -0,2369028 22006,93
Что можно улучшить в коде?