1 /*****************************************************************************
2 Major portions of this software are copyrighted by the Medical College
3 of Wisconsin, 1994-2000, and are released under the Gnu General Public
4 License, Version 2. See the file README.Copyright for details.
5 ******************************************************************************/
6
7 /*
8 This file contains routines to initialize and implement the
9 triangular wave (amplitude and phase parameters) signal model.
10
11 File: model_trnglrwave_ap.c
12 Author: B. Douglas Ward
13 Date: 29 May 1997
14 */
15
16
17 /*---------------------------------------------------------------------------*/
18
19 #include <math.h>
20 #include "NLfit_model.h"
21
22 void signal_model
23 (
24 float * gs, /* parameters for signal model */
25 int ts_length, /* length of time series data */
26 float ** x_array, /* independent variable matrix */
27 float * ts_array /* estimated signal model time series */
28 );
29
30
31 /*---------------------------------------------------------------------------*/
32 /*
33 Routine to initialize the signal model by defining the number of parameters
34 in the signal model, the name of the signal model, and the default values
35 for the minimum and maximum parameter constraints.
36 */
37
38 DEFINE_MODEL_PROTOTYPE
39
initialize_model()40 MODEL_interface * initialize_model ()
41 {
42 MODEL_interface * mi = NULL;
43
44
45 /*----- allocate memory space for model interface -----*/
46 mi = (MODEL_interface *) RwcMalloc (sizeof(MODEL_interface));
47
48
49 /*----- define interface for the triangular wave model -----*/
50
51 /*----- name of this model -----*/
52 strcpy (mi->label, "TrnglrWave_AP");
53
54 /*----- this is a signal model -----*/
55 mi->model_type = MODEL_SIGNAL_TYPE;
56
57 /*----- number of parameters in the model -----*/
58 mi->params = 2;
59
60 /*----- parameter labels -----*/
61 strcpy (mi->plabel[0], "amplitude");
62 strcpy (mi->plabel[1], "phase");
63
64 /*----- minimum and maximum parameter constraints -----*/
65 mi->min_constr[0] = -100.0; mi->max_constr[0] = 100.0;
66 mi->min_constr[1] = -90.0; mi->max_constr[1] = 0.00;
67
68 /*----- function which implements the model -----*/
69 mi->call_func = &signal_model;
70
71
72 /*----- return pointer to the model interface -----*/
73 return (mi);
74 }
75
76
77 /*---------------------------------------------------------------------------*/
78 /*
79 Routine to calculate the time series which results from using the
80 triangular wave model with specified amplitude and phase parameters.
81
82 Definition of model parameters:
83
84 gs[0] = amplitude of triangular wave
85 gs[1] = phase angle of triangular wave (degrees)
86 */
87
signal_model(float * gs,int ts_length,float ** x_array,float * ts_array)88 void signal_model
89 (
90 float * gs, /* parameters for signal model */
91 int ts_length, /* length of time series data */
92 float ** x_array, /* independent variable matrix */
93 float * ts_array /* estimated signal model time series */
94 )
95
96 {
97 const float FREQ = 0.125; /* frequency of the triangular wave */
98 int it; /* time index */
99 float t; /* time */
100 float fval; /* time series value at time t */
101 float cycles; /* number of cycles since initial time */
102
103
104 /*----- calculate time series corresponding to the given parameters -----*/
105 for (it = 0; it < ts_length; it++)
106 {
107 t = x_array[it][1];
108 cycles = FREQ*t + gs[1]/360.0;
109 cycles = cycles - (int)cycles;
110 if (cycles <= 0.25)
111 fval = gs[0] * (cycles/0.25);
112 else
113 if ((cycles > 0.25) && (cycles <= 0.75))
114 fval = gs[0] * (2.0 - (cycles/0.25));
115 else
116 fval = gs[0] * ((cycles/0.25) - 4.0);
117 ts_array[it] = fval;
118 }
119
120 }
121
122
123
124
125