gsl_fft__signals_source.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* fft/signals_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl_fft__signals.h"
  20. int
  21. FUNCTION(fft_signal,complex_pulse) (const size_t k,
  22. const size_t n,
  23. const size_t stride,
  24. const BASE z_real,
  25. const BASE z_imag,
  26. BASE data[],
  27. BASE fft[])
  28. {
  29. size_t j;
  30. if (n == 0)
  31. {
  32. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  33. }
  34. /* gsl_complex pulse at position k, data[j] = z * delta_{jk} */
  35. for (j = 0; j < n; j++)
  36. {
  37. REAL(data,stride,j) = 0.0;
  38. IMAG(data,stride,j) = 0.0;
  39. }
  40. REAL(data,stride,k % n) = z_real;
  41. IMAG(data,stride,k % n) = z_imag;
  42. /* fourier transform, fft[j] = z * exp(-2 pi i j k / n) */
  43. for (j = 0; j < n; j++)
  44. {
  45. const double arg = -2 * M_PI * ((double) ((j * k) % n)) / ((double) n);
  46. const BASE w_real = (BASE)cos (arg);
  47. const BASE w_imag = (BASE)sin (arg);
  48. REAL(fft,stride,j) = w_real * z_real - w_imag * z_imag;
  49. IMAG(fft,stride,j) = w_real * z_imag + w_imag * z_real;
  50. }
  51. return 0;
  52. }
  53. int
  54. FUNCTION(fft_signal,complex_constant) (const size_t n,
  55. const size_t stride,
  56. const BASE z_real,
  57. const BASE z_imag,
  58. BASE data[],
  59. BASE fft[])
  60. {
  61. size_t j;
  62. if (n == 0)
  63. {
  64. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  65. }
  66. /* constant, data[j] = z */
  67. for (j = 0; j < n; j++)
  68. {
  69. REAL(data,stride,j) = z_real;
  70. IMAG(data,stride,j) = z_imag;
  71. }
  72. /* fourier transform, fft[j] = n z delta_{j0} */
  73. for (j = 0; j < n; j++)
  74. {
  75. REAL(fft,stride,j) = 0.0;
  76. IMAG(fft,stride,j) = 0.0;
  77. }
  78. REAL(fft,stride,0) = ((BASE) n) * z_real;
  79. IMAG(fft,stride,0) = ((BASE) n) * z_imag;
  80. return 0;
  81. }
  82. int
  83. FUNCTION(fft_signal,complex_exp) (const int k,
  84. const size_t n,
  85. const size_t stride,
  86. const BASE z_real,
  87. const BASE z_imag,
  88. BASE data[],
  89. BASE fft[])
  90. {
  91. size_t j;
  92. if (n == 0)
  93. {
  94. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  95. }
  96. /* exponential, data[j] = z * exp(2 pi i j k) */
  97. for (j = 0; j < n; j++)
  98. {
  99. const double arg = 2 * M_PI * ((double) ((j * k) % n)) / ((double) n);
  100. const BASE w_real = (BASE)cos (arg);
  101. const BASE w_imag = (BASE)sin (arg);
  102. REAL(data,stride,j) = w_real * z_real - w_imag * z_imag;
  103. IMAG(data,stride,j) = w_real * z_imag + w_imag * z_real;
  104. }
  105. /* fourier transform, fft[j] = z * delta{(j - k),0} */
  106. for (j = 0; j < n; j++)
  107. {
  108. REAL(fft,stride,j) = 0.0;
  109. IMAG(fft,stride,j) = 0.0;
  110. }
  111. {
  112. int freq;
  113. if (k <= 0)
  114. {
  115. freq = (n-k) % n ;
  116. }
  117. else
  118. {
  119. freq = (k % n);
  120. };
  121. REAL(fft,stride,freq) = ((BASE) n) * z_real;
  122. IMAG(fft,stride,freq) = ((BASE) n) * z_imag;
  123. }
  124. return 0;
  125. }
  126. int
  127. FUNCTION(fft_signal,complex_exppair) (const int k1,
  128. const int k2,
  129. const size_t n,
  130. const size_t stride,
  131. const BASE z1_real,
  132. const BASE z1_imag,
  133. const BASE z2_real,
  134. const BASE z2_imag,
  135. BASE data[],
  136. BASE fft[])
  137. {
  138. size_t j;
  139. if (n == 0)
  140. {
  141. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  142. }
  143. /* exponential, data[j] = z1 * exp(2 pi i j k1) + z2 * exp(2 pi i j k2) */
  144. for (j = 0; j < n; j++)
  145. {
  146. const double arg1 = 2 * M_PI * ((double) ((j * k1) % n)) / ((double) n);
  147. const BASE w1_real = (BASE)cos (arg1);
  148. const BASE w1_imag = (BASE)sin (arg1);
  149. const double arg2 = 2 * M_PI * ((double) ((j * k2) % n)) / ((double) n);
  150. const BASE w2_real = (BASE)cos (arg2);
  151. const BASE w2_imag = (BASE)sin (arg2);
  152. REAL(data,stride,j) = w1_real * z1_real - w1_imag * z1_imag;
  153. IMAG(data,stride,j) = w1_real * z1_imag + w1_imag * z1_real;
  154. REAL(data,stride,j) += w2_real * z2_real - w2_imag * z2_imag;
  155. IMAG(data,stride,j) += w2_real * z2_imag + w2_imag * z2_real;
  156. }
  157. /* fourier transform, fft[j] = z1 * delta{(j - k1),0} + z2 *
  158. delta{(j - k2,0)} */
  159. for (j = 0; j < n; j++)
  160. {
  161. REAL(fft,stride,j) = 0.0;
  162. IMAG(fft,stride,j) = 0.0;
  163. }
  164. {
  165. int freq1, freq2;
  166. if (k1 <= 0)
  167. {
  168. freq1 = (n - k1) % n;
  169. }
  170. else
  171. {
  172. freq1 = (k1 % n);
  173. };
  174. if (k2 <= 0)
  175. {
  176. freq2 = (n - k2) % n;
  177. }
  178. else
  179. {
  180. freq2 = (k2 % n);
  181. };
  182. REAL(fft,stride,freq1) += ((BASE) n) * z1_real;
  183. IMAG(fft,stride,freq1) += ((BASE) n) * z1_imag;
  184. REAL(fft,stride,freq2) += ((BASE) n) * z2_real;
  185. IMAG(fft,stride,freq2) += ((BASE) n) * z2_imag;
  186. }
  187. return 0;
  188. }
  189. int
  190. FUNCTION(fft_signal,complex_noise) (const size_t n,
  191. const size_t stride,
  192. BASE data[],
  193. BASE fft[])
  194. {
  195. size_t i;
  196. int status;
  197. if (n == 0)
  198. {
  199. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  200. }
  201. for (i = 0; i < n; i++)
  202. {
  203. REAL(data,stride,i) = (BASE)urand();
  204. IMAG(data,stride,i) = (BASE)urand();
  205. }
  206. /* compute the dft */
  207. status = FUNCTION(gsl_dft_complex,forward) (data, stride, n, fft);
  208. return status;
  209. }
  210. int
  211. FUNCTION(fft_signal,real_noise) (const size_t n,
  212. const size_t stride,
  213. BASE data[],
  214. BASE fft[])
  215. {
  216. size_t i;
  217. int status;
  218. if (n == 0)
  219. {
  220. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  221. }
  222. for (i = 0; i < n; i++)
  223. {
  224. REAL(data,stride,i) = (BASE)urand();
  225. IMAG(data,stride,i) = 0.0;
  226. }
  227. /* compute the dft */
  228. status = FUNCTION(gsl_dft_complex,forward) (data, stride, n, fft);
  229. return status;
  230. }