-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSignal.cpp
More file actions
executable file
·264 lines (221 loc) · 5.92 KB
/
Signal.cpp
File metadata and controls
executable file
·264 lines (221 loc) · 5.92 KB
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
// Signal.cpp: implementation of the CSignal class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SpectrumAnalysis.h"
#include "Signal.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSignal::CSignal()
{
m_lSize = 0;
m_pSampleConsole = new SAMPLE[m_lSize +1];
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
m_lSampleFrequence = 0;
}
CSignal::CSignal(LONG in_Size)
{
if (in_Size > 0)
{
m_lSize = in_Size;
}
else
{
m_lSize = 0;
}
m_pSampleConsole = new SAMPLE[m_lSize + 1];
// set pointer-based array element
for ( int i = 0; i < m_lSize; i++ )
{
m_pSampleConsole[ i ] = 0;
}
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
m_lSampleFrequence = 0;
}
CSignal::CSignal(LONG in_Size, LONG in_lFrequency)
{
if (in_Size > 0)
{
m_lSize = in_Size;
}
else
{
m_lSize = 0;
}
m_pSampleConsole = new SAMPLE[m_lSize + 1];
// set pointer-based array element
for ( int i = 0; i < m_lSize; i++ )
{
m_pSampleConsole[ i ] = 0;
}
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
m_lSampleFrequence = in_lFrequency;
}
//////////////////////////////////////////////////////////////////////////
//copy constructor
CSignal::CSignal( const CSignal &in_SignalCopy)
:m_lSize(in_SignalCopy.m_lSize)
{
// create space for pointer-based array
m_pSampleConsole = new SAMPLE[ m_lSize + 1];
for ( int i = 0; i < m_lSize; i++ )
{
// copy into object
m_pSampleConsole[ i ] =
in_SignalCopy.m_pSampleConsole[ i ];
}
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
m_lSampleFrequence = in_SignalCopy.m_lSampleFrequence;
}
//////////////////////////////////////////////////////////////////////////
CSignal::~CSignal()
{
delete [] m_pSampleConsole;
}
//////////////////////////////////////////////////////////////////////////
LONG CSignal::GetSignalSize() const
{
return m_lSize;
}
//////////////////////////////////////////////////////////////////////////
const CSignal& CSignal::operator=( const CSignal &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side array, then allocate new left-side array
if ( m_lSize != right.m_lSize )
{
delete [] m_pSampleConsole; // release space
m_lSize = right.m_lSize; // resize this object
m_pSampleConsole = new SAMPLE[m_lSize + 1]; // create space for array copy
}
for ( int i = 0; i < m_lSize; i++ )
{
m_pSampleConsole[ i ] = right.m_pSampleConsole[ i ]; // copy array into object
}
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
m_lSampleFrequence = right.m_lSampleFrequence;
}
return *this; // enables x = y = z, for example
}
//////////////////////////////////////////////////////////////////////////
SAMPLE &CSignal::operator[](LONG subscript )
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= m_lSize )
{
TRACE(SPA_SGN_OUTOFRANGE);
return m_pSampleConsole[m_lSize];
}
return m_pSampleConsole[subscript];
}
//////////////////////////////////////////////////////////////////////////
SAMPLE CSignal::operator[](LONG subscript ) const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= m_lSize )
{
TRACE(SPA_SGN_OUTOFRANGE);
return m_pSampleConsole[m_lSize];
}
return m_pSampleConsole[subscript];
}
//////////////////////////////////////////////////////////////////////////
CSignal operator*(const CSignal& sgX, const CSignal& sgH)
{
CSignal snResult(0);
if(sgX.m_lSize != sgH.m_lSize)
{
TRACE(SPA_SGN_INVALID_CONVOLUTION);
return snResult;
}
//if two signal has the same size
LONG n = 0;
LONG k = 0;
LONG sizeN = sgX.m_lSize;
//prepare mem to store result
snResult.LocateMem(sizeN);
for (n=0; n <sizeN; n++)
{
SAMPLE nTemp = 0;
for(k=0; k< sizeN; k++)
{
nTemp += sgX[k] * sgH[n-k];
}
snResult[n] = nTemp;
}
return snResult;
}
//////////////////////////////////////////////////////////////////////////
// prepare memory for thi signal
BOOL CSignal::LocateMem(LONG in_size)
{
if(in_size < 0)
return FALSE;
else
{
m_lSize = in_size;
delete [] m_pSampleConsole;
m_pSampleConsole = NULL;
m_pSampleConsole = new SAMPLE[m_lSize+1];
ASSERT(m_pSampleConsole != NULL);
// set pointer-based array element
for ( int i = 0; i < m_lSize; i++ )
{
m_pSampleConsole[ i ] = 0;
}
m_pSampleConsole[m_lSize] = SPA_END_OF_SIGNAL;
return TRUE;
}
}
//////////////////////////////////////////////////////////////////////////
BOOL CSignal::IsEmpty()
{
if(m_lSize == 0) return TRUE;
else return FALSE;
}
//////////////////////////////////////////////////////////////////////////
LONG CSignal::GetFrequency() const
{
return m_lSampleFrequence;
}
//////////////////////////////////////////////////////////////////////////
VOID CSignal::SetFrequency(LONG in_Freq)
{
m_lSampleFrequence = in_Freq;
}
//////////////////////////////////////////////////////////////////////////
SAMPLE CSignal::GetMinSample()
{
ASSERT(!IsEmpty());
SAMPLE spMin = SHRT_MAX;
for (LONG i=0; i<m_lSize; i++)
{
SAMPLE spTem = m_pSampleConsole[i];
spMin = min(spMin, spTem);
}
return spMin;
}
//////////////////////////////////////////////////////////////////////////
SAMPLE CSignal::GetMaxSample()
{
ASSERT(!IsEmpty());
SAMPLE spMax = SHRT_MIN;
for (LONG i=0; i<m_lSize; i++)
{
SAMPLE spTem = m_pSampleConsole[i];
spMax = max(spMax, spTem);
}
return spMax;
}
//////////////////////////////////////////////////////////////////////////
VOID CSignal::Empty()
{
}