-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussian_chirplet_render.cpp
286 lines (213 loc) · 6.54 KB
/
gaussian_chirplet_render.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <vector>
#include <math.h>
#include <memory.h>
#include <pngwriter.h>
using namespace std;
#define access_mtx_value(mtx, x, y, xsize) mtx[y*xsize + x]
#ifndef BOOL
#define BOOL int
#endif
#ifndef TRUE
#define TRUE -1
#endif
#ifndef FALSE
#define FALSE 0
#endif
// helper functions
double square(double fValue);
inline double square(double fValue)
{
return fValue*fValue;
}
//! \brief This class represents a gaussian chirplet
class CGaussianChirplet
{
public:
//! \brief Contructor
CGaussianChirplet();
//! Calculate the value of the chirplet at time fTime
//!
//! \param[in] fTime the time to calculate the chirplet value
//! \return the chriplet value at time fTime
double value(double fTime) const;
//! this will be used to render the 2d image freq x time
//! still not implemented
double value2d(double fTime, double fFrequency) const;
public:
double m_fAmplitude;
// gaussian parameters {{
//! center of the gaussian
double m_fCentralTime;
//! sigma, the width of the gaussian
double m_fSigma;
// }} gaussian parameters
// the chirplet/oscilatory parameters {{
//! the radial frequency
double m_fw;
//! the phase of the oscilatory part
double m_fPhase;
//! the chrirp factor
double m_fc;
// }} the chirplet parameters
};
typedef vector<CGaussianChirplet> CGaussianChirpletVector;
CGaussianChirplet::CGaussianChirplet()
{
}
double CGaussianChirplet::value(double fTime) const
{
return m_fAmplitude*exp(-square((fTime - m_fCentralTime)/m_fSigma))*cos((m_fw + m_fc*fTime)*fTime + m_fPhase);
}
double CGaussianChirplet::value2d(double fTime, double fFrequency) const
{
return m_fAmplitude*exp(-(square((fTime - m_fCentralTime)/m_fSigma) + square((fFrequency - (m_fw + m_fc*((fTime - m_fCentralTime))))*m_fSigma)));
}
void render_image(unsigned int uiTimeSamples, unsigned int uiFrequencySamples, const double* pfImage, const char *pcszOutFile, BOOL bInverse = TRUE);
void render_image(unsigned int uiTimeSamples, unsigned int uiFrequencySamples, const double* pfImage, const char *pcszOutFile, BOOL bInverse)
{
pngwriter pngImage((int)uiTimeSamples, (int)uiFrequencySamples, 1.0, pcszOutFile);
unsigned int i, j;
for(i = 0; i < uiTimeSamples; i++)
{
for(j = 0; j < uiFrequencySamples; j++)
{
double fValue = access_mtx_value(pfImage, i, j, uiTimeSamples);
// secure a max of 1.0 for value
fValue = fValue <= 1.0 ? fValue : 1.0;
if(bInverse)
fValue = 1.0 - fValue;
pngImage.plot((int)i, (int)j, fValue, fValue, fValue);
}
}
pngImage.close();
}
//! Scales a double vector so that the maximum value is set to 1.0
void scale_to_max(double* pfValues, unsigned int nSize);
void scale_to_max(double* pfValues, unsigned int nSize)
{
unsigned int i;
double fBuffer = 0.0;
// find the max value
for(i = 0; i < nSize; i++)
{
if(pfValues[i] > fBuffer)
{
fBuffer = pfValues[i];
}
}
if(fBuffer != 0.0)
{
fBuffer = 1.0/fBuffer;
for(i = 0; i < nSize; i++)
{
pfValues[i] *= fBuffer;
}
}
}
//! this scales bla bla bla, implement later
double gaussian_adjust_intensity(double fValue)
{
return exp(-square((fValue-0.5)*10.0));
}
int main(int argc, const char *argv[])
{
if(argc < 1)
return 0;
unsigned int i, j, k;
unsigned int uiTimeSamples = 800;
unsigned int uiFrequencySamples = 480;
unsigned int uiMaxChirplets = 1024;
BOOL bUseAmplitude = TRUE;
BOOL bGaussianAdjustIntensity = FALSE;
BOOL bInverse = TRUE;
BOOL bScale = TRUE;
double fSamplingFrequency = 128.0;
double fTimeMin = 0.0;
double fTimeMax = 16.0;
double fFreqMin = 0.0;
double fFreqMax = 32.0;
FILE* pInFile;
CGaussianChirpletVector gcvector;
pInFile = fopen(argv[1], "rt");
if(!pInFile)
{
fprintf(stdout, "Impossible to open input file.\n");
}
// read the file {{
CGaussianChirpletVector::iterator it;
CGaussianChirplet gchirp;
float frbuffer[4];
int nrbuffer[2];
// while(fscanf(pInFile, "%e %e %e %e %e %e", &gchirp.m_fAmplitude, &gchirp.m_fSigma, &gchirp.m_fCentralTime, &gchirp.m_fw, &dummy[0], &gchirp.m_fc )!=EOF)
for(i = 0; fscanf(pInFile, "%e %d %d %e %e %e", &frbuffer[0], &nrbuffer[0], &nrbuffer[1], &frbuffer[1], &frbuffer[2], &frbuffer[3] )!=EOF; i++)
{
gchirp.m_fSigma = ((double)nrbuffer[0])/fSamplingFrequency;
gchirp.m_fCentralTime = ((double)nrbuffer[1])/fSamplingFrequency; // is sampling 128? probably
if(bUseAmplitude)
{
// gchirp.m_fAmplitude = sqrt(frbuffer[0]); // lets pretend everybody is equal for tests
gchirp.m_fAmplitude = sqrt(frbuffer[0]); // lets pretend everybody is equal for tests
}
else
{
gchirp.m_fAmplitude = 1.0; // lets pretend everybody is equal for tests
}
gchirp.m_fw = fSamplingFrequency*frbuffer[1]/(2*M_PI);
gchirp.m_fPhase = frbuffer[2];
gchirp.m_fc = fSamplingFrequency*fSamplingFrequency*frbuffer[3]/(2*M_PI);
/*
CGaussianChirplet test_chirp;
test_chirp.m_fAmplitude = 1.0;
test_chirp.m_fw = 32.0;
test_chirp.m_fc = 10;
test_chirp.m_fCentralTime = 4.0;
test_chirp.m_fSigma = 1.0;
*/
gcvector.push_back(gchirp);
i++;
if(i >= uiMaxChirplets)
break;
}
fclose(pInFile);
// }} read the file
// allocate the double bitmap
double *pfImage = new double[uiTimeSamples*uiFrequencySamples];
// reset the bitmap values
memset(pfImage, 0, uiTimeSamples*uiFrequencySamples*sizeof(double));
// steps in time
double fTimeStep = (fTimeMax - fTimeMin)/((double)uiTimeSamples);
// steps in frequency
double fFreqStep = (fFreqMax - fFreqMin)/((double)uiFrequencySamples);
// gaussian chirplet loop
// render to the float bitmap
for(k = 0; k < gcvector.size(); k++)
{
// time loop
for(i = 0; i < uiTimeSamples; i++)
{
// frequency loop
for(j = 0; j < uiFrequencySamples; j++)
{
double fTime = fTimeMin + ((double)i)*fTimeStep;
double fFreq = fFreqMin + ((double)j)*fFreqStep;
// fTime =
// fFreq =
double fValue = gcvector[k].value2d(fTime, fFreq);
double fScaledValue = fValue / gcvector[k].m_fAmplitude;
// interesting cosmetic, just draw values around 0.5 of the max
// if(fNormalValue >= 0.4 && fNormalValue <= 0.6)
// access_mtx_value(pfImage, i, j, uiTimeSamples) += fValue;
access_mtx_value(pfImage, i, j, uiTimeSamples) += bGaussianAdjustIntensity ? (bUseAmplitude ? gcvector[k].m_fAmplitude : 1.0) * gaussian_adjust_intensity(fScaledValue) : fValue;
}
}
}
// scale
if(bScale)
{
scale_to_max(pfImage, uiTimeSamples*uiFrequencySamples);
}
render_image(uiTimeSamples, uiFrequencySamples, pfImage, argv[2], bInverse);
// TODO: here you need to write the bitmap
return 0;
}