libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
psd_periodogram_cmplx.c
1 /*
2 * Copyright (c) 2015-2024 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of libdspl-2.0.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <float.h>
25 #include "dspl.h"
26 
27 
28 
29 #ifdef DOXYGEN_ENGLISH
30 
31 #endif
32 #ifdef DOXYGEN_RUSSIAN
33 
147 #endif
148 int DSPL_API psd_periodogram_cmplx(complex_t* x, int n,
149  int win_type, double win_param,
150  fft_t* pfft, double fs,
151  int flag, double* ppsd, double* pfrq)
152 {
153  double *w = NULL;
154  complex_t *s = NULL;
155  double u, wn;
156  int err, k;
157  fft_t *ptr_fft = NULL;
158 
159  if(!x || !ppsd)
160  return ERROR_PTR;
161 
162  if(n<1 )
163  return ERROR_SIZE;
164 
165  if(fs < 0.0)
166  return ERROR_FS;
167 
168  if(!pfft)
169  {
170  ptr_fft = (fft_t*)malloc(sizeof(fft_t));
171  memset(ptr_fft, 0, sizeof(fft_t));
172  }
173  else
174  ptr_fft = pfft;
175 
176 
177  if(win_type != DSPL_WIN_RECT)
178  {
179  /* Modified periodogram calculation */
180 
181  /* window malloc */
182  w = (double*)malloc(n*sizeof(double));
183  if(!w)
184  {
185  err = ERROR_MALLOC;
186  goto exit_label;
187  }
188 
189  /* create window */
190  err = window(w, n, win_type, win_param);
191  if(err != RES_OK)
192  goto exit_label;
193 
194  /* window normalization wn = sum(w.^2) */
195  wn = 0;
196  for(k = 0; k < n; k++)
197  wn += w[k]*w[k];
198 
199  /* signal buffer malloc */
200  s = (complex_t*)malloc(n*sizeof(complex_t));
201  if(!s)
202  {
203  err = ERROR_MALLOC;
204  goto exit_label;
205  }
206 
207  /* windowing */
208  for(k = 0; k < n; k++)
209  {
210  RE(s[k]) = RE(x[k]) * w[k];
211  IM(s[k]) = IM(x[k]) * w[k];
212  }
213  }
214  else
215  {
216  /* classic periodogram without windowing */
217  s = x;
218  wn = (double)n;
219  }
220 
221  /* calculate FFT */
222  err = fft_mag_cmplx(s, n, ptr_fft, fs, flag, ppsd, pfrq);
223  if(err != RES_OK)
224  goto exit_label;
225 
226 
227  if(flag & DSPL_FLAG_LOGMAG)
228  {
229  /* normalization in log scale */
230  u = 10.0 * log10(wn * fs);
231  for(k = 0; k < n; k++)
232  ppsd[k] -= u;
233  }
234  else
235  {
236  /* normalization in linear scale */
237  u = 1.0 / (wn * fs);
238  for(k = 0; k < n; k++)
239  ppsd[k] *= u;
240  }
241 
242 exit_label:
243  if(w)
244  free(w);
245  if(s && s != x)
246  free(s);
247  if(ptr_fft && (ptr_fft != pfft))
248  {
249  fft_free(ptr_fft);
250  free(ptr_fft);
251  }
252  return err;
253 }
254 
int window(double *w, int n, int win_type, double param)
Расчет функции оконного взвешивания
Definition: win.c:329
#define RE(x)
Макрос определяющий реальную часть комплексного числа.
Definition: dspl.h:420
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
void fft_free(fft_t *pfft)
Очистить структуру fft_t алгоритма БПФ
Definition: fft_free.c:61
int psd_periodogram_cmplx(complex_t *x, int n, int win_type, double win_param, fft_t *pfft, double fs, int flag, double *ppsd, double *pfrq)
Непараметрическая оценка спектральной плотности мощности (СПМ) комплексного сигнала методом модифицир...
Структура данных объекта быстрого преобразования Фурье
Definition: dspl.h:277
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
#define IM(x)
Макрос определяющий мнимую часть комплексного числа.
Definition: dspl.h:478
#define ERROR_MALLOC
Ошибка динамического выделения памяти. Данная ошибка означает, что при динамическом выделении памяти ...
Definition: dspl.h:599