Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. <controls:PanoramaItem Header="Graphic">
  2. <StackPanel>
  3. <Canvas Visibility="Visible" Width="530" VerticalAlignment="Center">
  4. <TextBlock Text="|0" Canvas.Left="0" Canvas.Top="380"/>
  5. <TextBlock Text="|50" Canvas.Left="25" Canvas.Top="380"/>
  6. <TextBlock Text="|100" Canvas.Left="50" Canvas.Top="380"/>
  7. <TextBlock Text="|200" Canvas.Left="100" Canvas.Top="380"/>
  8. <TextBlock Text="|300" Canvas.Left="150" Canvas.Top="380"/>
  9. <TextBlock Text="|440" Canvas.Left="220" Canvas.Top="380"/>
  10.  
  11. <TextBlock Text="|500" Canvas.Left="250" Canvas.Top="380"/>
  12. <TextBlock Text="|600" Canvas.Left="300" Canvas.Top="380"/>
  13. <TextBlock Text="|700" Canvas.Left="350" Canvas.Top="380"/>
  14. <Polygon x:Name="graph"
  15. Points="0,150 400,125 400,275 300,200"
  16. Stroke="Brown"
  17. StrokeThickness="0.4" Canvas.Left="0" Canvas.Top="0">
  18. <Polygon.Fill>
  19. <SolidColorBrush Color="White" Opacity="0.9"/>
  20. </Polygon.Fill>
  21. </Polygon>
  22. </Canvas>
  23. </StackPanel>
  24. </controls:PanoramaItem>
  25.  
  26. double[] spectr = FftAlgorithm.Calculate(x);// To have the spectr
  27. myPointCollection.Add(new System.Windows.Point(0, 370));
  28. for(int i = 0; i < spectr.Length;i+= 2)
  29. {
  30. myPointCollection.Add(new System.Windows.Point(0+i/2, 370 - spectr[i]/2));
  31. if (i >= 400*1)
  32. {
  33. myPointCollection.Add(new System.Windows.Point(0+455/4,370));
  34. break;
  35. }
  36. }
  37.  
  38.  
  39. graph.Points = myPointCollection;
  40.  
  41. struct ComplexNumber
  42. {
  43. public double Re;
  44. public double Im;
  45.  
  46. public ComplexNumber(double re)
  47. {
  48. this.Re = re;
  49. this.Im = 0;
  50. }
  51.  
  52. public ComplexNumber(double re, double im)
  53. {
  54. this.Re = re;
  55. this.Im = im;
  56. }
  57.  
  58. public static ComplexNumber operator *(ComplexNumber n1, ComplexNumber n2)
  59. {
  60. return new ComplexNumber(n1.Re * n2.Re - n1.Im * n2.Im,
  61. n1.Im * n2.Re + n1.Re * n2.Im);
  62. }
  63.  
  64. public static ComplexNumber operator +(ComplexNumber n1, ComplexNumber n2)
  65. {
  66. return new ComplexNumber(n1.Re + n2.Re, n1.Im + n2.Im);
  67. }
  68.  
  69. public static ComplexNumber operator -(ComplexNumber n1, ComplexNumber n2)
  70. {
  71. return new ComplexNumber(n1.Re - n2.Re, n1.Im - n2.Im);
  72. }
  73.  
  74. public static ComplexNumber operator -(ComplexNumber n)
  75. {
  76. return new ComplexNumber(-n.Re, -n.Im);
  77. }
  78.  
  79. public static implicit operator ComplexNumber(double n)
  80. {
  81. return new ComplexNumber(n, 0);
  82. }
  83.  
  84. public ComplexNumber PoweredE()
  85. {
  86. double e = Math.Exp(Re);
  87. return new ComplexNumber(e * Math.Cos(Im), e * Math.Sin(Im));
  88. }
  89.  
  90. public double Power2()
  91. {
  92. return Re * Re - Im * Im;
  93. }
  94.  
  95. public double AbsPower2()
  96. {
  97. return Re * Re + Im * Im;
  98. }
  99.  
  100. public override string ToString()
  101. {
  102. return String.Format("{0}+i*{1}", Re, Im);
  103. }
  104. }
  105.  
  106. public static class FftAlgorithm
  107. {
  108. /// <summary>
  109. /// Calculates FFT using Cooley-Tukey FFT algorithm.
  110. /// </summary>
  111. /// <param name="x">input data</param>
  112. /// <returns>spectrogram of the data</returns>
  113. /// <remarks>
  114. /// If amount of data items not equal a power of 2, then algorithm
  115. /// automatically pad with 0s to the lowest amount of power of 2.
  116. /// </remarks>
  117. public static double[] Calculate(double[] x)
  118. {
  119. int length;
  120. int bitsInLength;
  121. if (IsPowerOfTwo(x.Length))
  122. {
  123. length = x.Length;
  124. bitsInLength = Log2(length) - 1;
  125. }
  126. else
  127. {
  128. bitsInLength = Log2(x.Length);
  129. length = 1 << bitsInLength;
  130. // the items will be pad with zeros
  131. }
  132.  
  133. // bit reversal
  134. ComplexNumber[] data = new ComplexNumber[length];
  135. for (int i = 0; i < x.Length; i++)
  136. {
  137. int j = ReverseBits(i, bitsInLength);
  138. data[j] = new ComplexNumber(x[i]);
  139. }
  140.  
  141. // Cooley-Tukey
  142. for (int i = 0; i < bitsInLength; i++)
  143. {
  144. int m = 1 << i;
  145. int n = m * 2;
  146. double alpha = -(2 * Math.PI / n);
  147.  
  148. for (int k = 0; k < m; k++)
  149. {
  150. // e^(-2*pi/N*k)
  151. ComplexNumber oddPartMultiplier = new ComplexNumber(0, alpha * k).PoweredE();
  152.  
  153. for (int j = k; j < length; j += n)
  154. {
  155. ComplexNumber evenPart = data[j];
  156. ComplexNumber oddPart = oddPartMultiplier * data[j + m];
  157. data[j] = evenPart + oddPart;
  158. data[j + m] = evenPart - oddPart;
  159. }
  160. }
  161. }
  162.  
  163. // calculate spectrogram
  164. double[] spectrogram = new double[length];
  165. for (int i = 0; i < spectrogram.Length; i++)
  166. {
  167.  
  168. spectrogram[i] = data[i].AbsPower2();
  169.  
  170. }
  171. return spectrogram;
  172. }
  173.  
  174. /// <summary>
  175. /// Gets number of significat bytes.
  176. /// </summary>
  177. /// <param name="n">Number</param>
  178. /// <returns>Amount of minimal bits to store the number.</returns>
  179. private static int Log2(int n)
  180. {
  181. int i = 0;
  182. while (n > 0)
  183. {
  184. ++i; n >>= 1;
  185. }
  186. return i;
  187. }
  188.  
  189. /// <summary>
  190. /// Reverses bits in the number.
  191. /// </summary>
  192. /// <param name="n">Number</param>
  193. /// <param name="bitsCount">Significant bits in the number.</param>
  194. /// <returns>Reversed binary number.</returns>
  195. private static int ReverseBits(int n, int bitsCount)
  196. {
  197. int reversed = 0;
  198. for (int i = 0; i < bitsCount; i++)
  199. {
  200. int nextBit = n & 1;
  201. n >>= 1;
  202.  
  203. reversed <<= 1;
  204. reversed |= nextBit;
  205. }
  206. return reversed;
  207. }
  208.  
  209. /// <summary>
  210. /// Checks if number is power of 2.
  211. /// </summary>
  212. /// <param name="n">number</param>
  213. /// <returns>true if n=2^k and k is positive integer</returns>
  214. private static bool IsPowerOfTwo(int n)
  215. {
  216. return n > 1 && (n & (n - 1)) == 0;
  217. }
  218. }
  219.  
  220. Array
  221. 7774,84102428705
  222. 3,77541922071645
  223. 2,60171097464947
  224. 4,25540621691683
  225. 5,28851887723449
  226. 3,4148780139543
  227. 2,76973970084357
  228. 1,99548270720925
  229. 2,48089729433758
  230. 1,81148493997232
  231. 4,08591050588941
  232. 3,86634237956173
  233. 2,23154042032444
  234. 2,34943177165949
  235. 0,202241877529014
  236. 0,361856279722826
  237. 0,305770091815949
  238. 1,10819055376782
  239. 1,14078528143499
  240. 0,0885925819196023
  241. 0,0589821600576737
  242. 1,82627322162955
  243. 0,510590967254011
  244. 1,09703702271706
  245. 0,624027733620266
  246. 1,38613991581386
  247. 0,61563282026637
  248. 0,833719920552669
  249. 1,72066078720428
  250. 0,61866640580564
  251. 0,311134872285707
  252. 0,545134957482531
  253. 1,32248892387885
  254. 0,280664970077368
  255. 0,288037724854005
  256. 0,478421643868339
  257. 0,0965479866862064
  258. 2,77251684855075
  259. 4,28321499123499
  260. 0,498259034734298
  261. 9,93132231440244
  262. 2,10269510682998
  263. 0,805331883010264
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement