Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AverageSpectrumTest.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Bernd Machenschalk, Duncan Brown, Jolien Creighton
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <lal/LALStdlib.h>
24#include <lal/AVFactories.h>
25#include <lal/TimeFreqFFT.h>
26#include <lal/RealFFT.h>
27#include <lal/Window.h>
28#include <lal/Random.h>
29
30#define TESTSTATUS( s ) \
31 if ( (s)->statusCode ) { REPORTSTATUS( s ); exit( 1 ); } else \
32((void)0)
33
34int main( void )
35{
36 const UINT4 n = 65536;
37 const UINT4 m = 8;
38 static REAL4FrequencySeries fseries;
39 static REAL4TimeSeries tseries;
40 static LALStatus status;
41 static RandomParams *randpar;
42 REAL4FFTPlan *plan;
43 REAL4Window *window;
44 REAL8 ave;
45 UINT4 i;
46
47
48 /* allocate memory for time and frequency series */
49 tseries.deltaT = 1;
50 LALCreateVector( &status, &tseries.data, n * m );
52 LALCreateVector( &status, &fseries.data, n / 2 + 1 );
54
55 /* set time series data to be a unit impulse */
56 /*
57 memset( tseries.data->data, 0, tseries.data->length * sizeof(
58 *tseries.data->data ) );
59 tseries.data->data[0] = 1;
60 */
61 randpar = XLALCreateRandomParams( 1 );
62 XLALNormalDeviates( tseries.data, randpar );
63 XLALDestroyRandomParams( randpar );
64
65 /* prepare average spectrum parameters */
66 /* specpar.overlap = 0; */
67 plan = XLALCreateForwardREAL4FFTPlan( n, 0 );
69
70 /* compute spectrum */
71 XLALREAL4AverageSpectrumMedian( &fseries, &tseries, n, n / 2, window, plan );
72
73 /* output results -- omit DC & Nyquist */
74 /*
75 for ( i = 1; i < fseries.data->length - 1; ++i )
76 fprintf( stdout, "%e\t%e\n", i * fseries.deltaF,
77 fseries.data->data[i] );
78 */
79
80 /* average values of power spectrum (omit DC & Nyquist ) */
81 ave = 0;
82 for ( i = 1; i < fseries.data->length - 1; ++i )
83 ave += fseries.data->data[i];
84 ave /= fseries.data->length - 2;
85 fprintf( stdout, "median:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 );
86
87 /* now do the same for mean */
88 XLALREAL4AverageSpectrumWelch( &fseries, &tseries, n, n / 2, window, plan );
89 /* average values of power spectrum (omit DC & Nyquist ) */
90 ave = 0;
91 for ( i = 1; i < fseries.data->length - 1; ++i )
92 ave += fseries.data->data[i];
93 ave /= fseries.data->length - 2;
94 fprintf( stdout, "mean:\t%e\terror:\t%f%%\n", ave, fabs( ave - 2.0 ) / 0.02 );
95
96
97 /* cleanup */
98 XLALDestroyREAL4Window( window );
100 TESTSTATUS( &status );
101 LALDestroyVector( &status, &fseries.data );
102 TESTSTATUS( &status );
103 LALDestroyVector( &status, &tseries.data );
104 TESTSTATUS( &status );
105
106 /* exit */
108 return 0;
109}
int main(void)
#define TESTSTATUS(s)
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
#define fprintf
double REAL8
Double precision real floating-point number (8 bytes).
uint32_t UINT4
Four-byte unsigned integer.
static const INT4 m
Definition: Random.c:80
RandomParams * XLALCreateRandomParams(INT4 seed)
Definition: Random.c:102
void XLALDestroyRandomParams(RandomParams *params)
Definition: Random.c:140
int XLALNormalDeviates(REAL4Vector *deviates, RandomParams *params)
Definition: Random.c:171
REAL4FFTPlan * XLALCreateForwardREAL4FFTPlan(UINT4 size, int measurelvl)
Returns a new REAL4FFTPlan for a forward transform.
Definition: CudaRealFFT.c:120
void XLALDestroyREAL4FFTPlan(REAL4FFTPlan *plan)
Destroys a REAL4FFTPlan.
Definition: CudaRealFFT.c:140
int XLALREAL4AverageSpectrumMedian(REAL4FrequencySeries *spectrum, const REAL4TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL4Window *window, const REAL4FFTPlan *plan)
Median Method: use median average rather than mean.
int XLALREAL4AverageSpectrumWelch(REAL4FrequencySeries *spectrum, const REAL4TimeSeries *tseries, UINT4 seglen, UINT4 stride, const REAL4Window *window, const REAL4FFTPlan *plan)
Use Welch's method to compute the average power spectrum of a time series.
void LALDestroyVector(LALStatus *, REAL4Vector **)
void LALCreateVector(LALStatus *, REAL4Vector **, UINT4)
void XLALDestroyREAL4Window(REAL4Window *window)
Definition: Window.c:757
REAL4Window * XLALCreateWelchREAL4Window(UINT4 length)
Definition: Window.c:697
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
See DATATYPE-FrequencySeries types for documentation.
Definition: LALDatatypes.h:879
REAL4Sequence * data
Definition: LALDatatypes.h:885
Time series of REAL4 data, see DATATYPE-TimeSeries types for more details.
Definition: LALDatatypes.h:570
REAL4Sequence * data
The sequence of sampled data.
Definition: LALDatatypes.h:576
REAL8 deltaT
The time step between samples of the time series in seconds.
Definition: LALDatatypes.h:573
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:149
Structure for storing REAL4 window function data, providing storage for a sequence of samples as well...
Definition: Window.h:243
This structure contains the parameters necessary for generating the current sequence of random number...
Definition: Random.h:86