Source

Channel 3 / ReduceDialog.cxx

Full commit
#include "ReduceDialog.h"

enum{
	ID_average,
	ID_deviation,
};

BEGIN_EVENT_TABLE(ReduceDialog, wxFrame)
	EVT_BUTTON(wxID_OK, ReduceDialog::OnOK)
	EVT_SPINCTRL(wxID_ANY, ReduceDialog::OnMarkerChange)
END_EVENT_TABLE()

void ReduceDialog::OnOK(wxCommandEvent& event){
	Show(false);
}

void ReduceDialog::updateSpinners() {
	int startVal = startInput->GetValue();
	int endVal = endInput->GetValue();
	startInput->SetRange(0, endVal);
	endInput->SetRange(startVal+1, plot->horizontalMarkers.size()+1);
}

void ReduceDialog::updateStats() {
	int startMarker = startInput->GetValue();
	int endMarker = endInput->GetValue();
	int startPos, endPos;
	if (startMarker == 0) startPos = 0;
	else startPos = plot->horizontalMarkers[startMarker-1];
	if (endMarker == plot->horizontalMarkers.size()+1) endPos = plot->standardFile.dataPoints.size();
	else endPos = plot->horizontalMarkers[endMarker-1];
	float sumX = 0;
	float sumSquare = 0;
	for (int i = startPos; i < endPos; i++) {
		float voltage = ((float)plot->standardFile.dataPoints[i] * 20 * 1000) / 
			(((1 << plot->standardFile.databits)-1) * plot->standardFile.gainfactor);
		sumX += voltage;
		sumSquare += voltage*voltage;
	}
	avgText->SetValue(_(""));
	devText->SetValue(_(""));
	*avgText << sumX / (endPos-startPos) - plot->verticalMarkers[1];
	*devText << sqrt(sumSquare/(endPos-startPos) - pow(sumX / (endPos-startPos), 2));
}

void ReduceDialog::OnMarkerChange(wxSpinEvent& event) {
	updateSpinners();
	updateStats();
}

ReduceDialog::ReduceDialog(wxWindow *parent, PlotBox *plot)
	: wxFrame(parent, -1, _("Statistics"), wxDefaultPosition, wxDefaultSize,
			wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxSYSTEM_MENU | wxCAPTION | wxCLIP_CHILDREN),
	plot(plot)
{
	panel = new wxPanel(this, wxID_ANY);
	wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 5, 10);

	sizer->SetSizeHints(panel);
	panel->SetSizer(sizer);
	sizer->Add(new wxStaticText(panel, wxID_ANY, _("Average: ")), 1, wxLEFT|wxTOP, 10);
	sizer->Add(avgText = new wxTextCtrl(panel, wxID_ANY, _("...")), 0, wxRIGHT|wxTOP, 10);
	sizer->Add(new wxStaticText(panel, wxID_ANY, _("Std. Dev: ")), 1, wxLEFT, 10);
	sizer->Add(devText = new wxTextCtrl(panel, wxID_ANY, _("...")), 0, wxRIGHT, 10);
	sizer->Add(new wxStaticText(panel, wxID_ANY, _("From")), 1, wxLEFT, 10);
	sizer->Add(startInput = new wxSpinCtrl(panel, wxID_ANY, _("0")), 0, wxRIGHT, 10);	
	sizer->Add(new wxStaticText(panel, wxID_ANY, _("To")), 1, wxLEFT, 10);
	sizer->Add(endInput = new wxSpinCtrl(panel, wxID_ANY, _("1")), 0, wxRIGHT, 10);
	avgText->SetEditable(false);
	devText->SetEditable(false);
	updateSpinners();
	updateStats();

	wxStdDialogButtonSizer *buttonSizer = new wxStdDialogButtonSizer();
	buttonSizer->AddButton(new wxButton(panel, wxID_OK, _("OK")));
	buttonSizer->Realize();
	sizer->Add(buttonSizer, 0, wxSHAPED|wxBOTTOM|wxTOP, 5);

	panel->Fit();
	Fit();
	Centre();
}