forked from NOVACProject/MobileDOAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShowFitDlg.cpp
240 lines (190 loc) · 6.29 KB
/
ShowFitDlg.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
// ShowFitDlg.cpp : implementation file
//
#undef min
#undef max
#include "stdafx.h"
#include "DMSpec.h"
#include "ShowFitDlg.h"
#include <algorithm>
// CShowFitDlg dialog
using namespace Dialogs;
IMPLEMENT_DYNAMIC(CShowFitDlg, CDialog)
CShowFitDlg::CShowFitDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShowFitDlg::IDD, pParent)
{
fVisible = false;
}
CShowFitDlg::~CShowFitDlg()
{
}
void CShowFitDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CShowFitDlg, CDialog)
ON_WM_SIZE()
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CShowFitDlg message handlers
// CRealTimeRoute message handlers
BOOL CShowFitDlg::OnInitDialog()
{
CDialog::OnInitDialog();
for(int i = 0; i < MAX_SPECTRUM_LENGTH; ++i)
pixel[i] = i;
CRect rect;
this->GetClientRect(&rect);
int margin = 0;
rect.right -= margin;
rect.bottom -= margin;
rect.left = margin;
rect.top = margin;
m_fitPlot.Create(WS_VISIBLE | WS_CHILD, rect, this);
m_fitPlot.SetYUnits("Intensity");
m_fitPlot.SetXUnits("Pixel");
m_fitPlot.EnableGridLinesX();
m_fitPlot.EnableGridLinesY();
m_fitPlot.SetBackgroundColor(RGB(0, 0, 0));
m_fitPlot.SetGridColor(RGB(255,255,255));
m_fitPlot.SetRange(0,200,0,-1.0,1.0,3);
m_fitPlot2.Create(WS_VISIBLE | WS_CHILD, rect, this);
m_fitPlot2.SetYUnits("Intensity");
m_fitPlot2.SetXUnits("Pixel");
m_fitPlot2.EnableGridLinesX();
m_fitPlot2.EnableGridLinesY();
m_fitPlot2.SetBackgroundColor(RGB(0, 0, 0));
m_fitPlot2.SetGridColor(RGB(255,255,255));
m_fitPlot2.SetRange(0,200,0,-1.0,1.0,3);
DrawFit();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CShowFitDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(nType != SIZE_RESTORED)
return;
if(IsWindow(m_fitPlot.m_hWnd)){
if(this->m_spectrometer != nullptr && m_spectrometer->GetFitRegionNum() > 1){
m_fitPlot.MoveWindow(0, 0, cx, cy/2, FALSE);
m_fitPlot.CleanPlot();
m_fitPlot2.MoveWindow(0, cy/2, cx, cy/2, FALSE);
m_fitPlot2.CleanPlot();
}else{
m_fitPlot.MoveWindow(0, 0, cx, cy, FALSE);
m_fitPlot.CleanPlot();
}
}
DrawFit();
}
BOOL CShowFitDlg::Create(UINT nID, CWnd* pParentWnd){
// TODO: Add your specialized code here and/or call the base class
fVisible = false;
this->m_spectrometer = 0;
return CDialog::Create(nID, pParentWnd);
}
void CShowFitDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
fVisible = false;
this->DestroyWindow();
CDialog::OnClose();
}
void CShowFitDlg::DrawFit(){
DrawFit1();
DrawFit2();
}
void CShowFitDlg::DrawFit1(){
double marginSpace = 1e-5;
static double spectrum[MAX_SPECTRUM_LENGTH];
static double fitResult[MAX_SPECTRUM_LENGTH];
static double residual[MAX_SPECTRUM_LENGTH];
if (m_spectrometer == nullptr)
return;
int fitLow = m_spectrometer->GetFitLow();
int fitHigh = m_spectrometer->GetFitHigh();
int fitWidth = fitHigh - fitLow;
double minV = 1000;
double maxV = -1000;
static double oldMinV = 1e16, oldMaxV = -1e16;
// copy the high pass filtered spectrum to the local variable
m_spectrometer->GetProcessedSpectrum(spectrum, MAX_SPECTRUM_LENGTH, 0);
// copy the fitted result to the local variable
memcpy(fitResult, &m_spectrometer->m_fitResult[0], MAX_SPECTRUM_LENGTH*sizeof(double));
// find the minimum and maximum value
for(int i = fitLow; i < fitHigh; ++i){
minV = std::min(minV, spectrum[i]);
minV = std::min(minV, fitResult[i]);
maxV = std::max(maxV, spectrum[i]);
maxV = std::max(maxV, fitResult[i]);
}
if ((maxV - minV) < 0.25 * (oldMaxV - oldMinV)) {
oldMaxV = maxV;
oldMinV = minV;
}
else {
if (minV < oldMinV)
oldMinV = minV;
else
minV = oldMinV;
if (maxV > oldMaxV)
oldMaxV = maxV;
else
maxV = oldMaxV;
}
// set the range for the fit
m_fitPlot.SetRange(fitLow, fitHigh, 0, minV - marginSpace, maxV + marginSpace, 3);
// draw the spectrum
m_fitPlot.SetPlotColor(RGB(255,0,0));
m_fitPlot.XYPlot(pixel + fitLow, spectrum + fitLow, (int)fitWidth, Graph::CGraphCtrl::PLOT_CONNECTED);
// draw the fitted result
m_fitPlot.SetPlotColor(RGB(0, 255, 255));
m_fitPlot.XYPlot(pixel + fitLow, fitResult + fitLow, (int)fitWidth, Graph::CGraphCtrl::PLOT_CONNECTED | Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
void CShowFitDlg::DrawFit2(){
double marginSpace = 1e-5;
static double spectrum[MAX_SPECTRUM_LENGTH];
static double fitResult[MAX_SPECTRUM_LENGTH];
static double residual[MAX_SPECTRUM_LENGTH];
if (m_spectrometer == nullptr || m_spectrometer->GetFitRegionNum() == 1)
return;
int fitLow = m_spectrometer->GetFitLow(1);
int fitHigh = m_spectrometer->GetFitHigh(1);
int fitWidth = fitHigh - fitLow;
double minV = 1000;
double maxV = -1000;
static double oldMinV = 1e16, oldMaxV = -1e16;
// copy the high pass filtered spectrum to the local variable
m_spectrometer->GetProcessedSpectrum(spectrum, MAX_SPECTRUM_LENGTH, 1);
// copy the fitted result to the local variable
memcpy(fitResult, &m_spectrometer->m_fitResult[1], MAX_SPECTRUM_LENGTH*sizeof(double));
// find the minimum and maximum value
for(int i = fitLow; i < fitHigh; ++i){
minV = std::min(minV, spectrum[i]);
minV = std::min(minV, fitResult[i]);
maxV = std::max(maxV, spectrum[i]);
maxV = std::max(maxV, fitResult[i]);
}
if ((maxV - minV) < 0.25 * (oldMaxV - oldMinV)) {
oldMaxV = maxV;
oldMinV = minV;
}
else {
if (minV < oldMinV)
oldMinV = minV;
else
minV = oldMinV;
if (maxV > oldMaxV)
oldMaxV = maxV;
else
maxV = oldMaxV;
}
// set the range for the fit
m_fitPlot2.SetRange(fitLow, fitHigh, 0, minV - marginSpace, maxV + marginSpace, 3);
// draw the spectrum
m_fitPlot2.SetPlotColor(RGB(255,0,0));
m_fitPlot2.XYPlot(pixel + fitLow, spectrum + fitLow, (int)fitWidth, Graph::CGraphCtrl::PLOT_CONNECTED);
// draw the fitted result
m_fitPlot2.SetPlotColor(RGB(0, 255, 255));
m_fitPlot2.XYPlot(pixel + fitLow, fitResult + fitLow, (int)fitWidth, Graph::CGraphCtrl::PLOT_CONNECTED | Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}