unit DataSmoothUnit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, MainUnit, Globals, functionsLib, OutPutUnit, DataProcs, DictionaryUnit, contexthelpunit; type { TSmoothDataForm } TSmoothDataForm = class(TForm) CancelBtn: TButton; HelpBtn: TButton; Label3: TLabel; ComputeBtn: TButton; RepeatEdit: TEdit; Label1: TLabel; ResetBtn: TButton; VariableEdit: TEdit; InBtn: TBitBtn; Label2: TLabel; Memo1: TMemo; OutBtn: TBitBtn; VarList: TListBox; procedure ComputeBtnClick(Sender: TObject); procedure InBtnClick(Sender: TObject); procedure OutBtnClick(Sender: TObject); procedure ResetBtnClick(Sender: TObject); private { private declarations } public { public declarations } end; var SmoothDataForm: TSmoothDataForm; implementation { TSmoothDataForm } procedure TSmoothDataForm.ResetBtnClick(Sender: TObject); VAR i : integer; begin VarList.Clear; for i := 1 to NoVariables do VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); RepeatEdit.Text := '1'; VariableEdit.Text := ''; InBtn.Visible := true; OutBtn.Visible := false; end; procedure TSmoothDataForm.InBtnClick(Sender: TObject); VAR index : integer; begin index := VarList.ItemIndex; VariableEdit.Text := VarList.Items.Strings[index]; VarList.Items.Delete(index); InBtn.Visible := false; OutBtn.Visible := true; end; procedure TSmoothDataForm.ComputeBtnClick(Sender: TObject); VAR DataPts, OutPts : DblDyneVec; value, dblvalue, avg : double; VarCol, result, N, Reps, intvalue, i, j, col : integer; varlabel, strvalue : string; begin N := NoCases; SetLength(DataPts,N); SetLength(OutPts,N); Reps := StrToInt(RepeatEdit.Text); varlabel := VariableEdit.Text; for i := 1 to NoVariables do if varlabel = OS3MainFrm.DataGrid.Cells[i,0] then VarCol := i; for i := 1 to N do begin value := StrToFloat(OS3MainFrm.DataGrid.Cells[VarCol,i]); DataPts[i-1] := value; end; // repeat smoothing for Reps times OutPts[0] := DataPts[0]; OutPts[N-1] := DataPts[N-1]; for j := 1 to Reps do begin for i := 1 to N-2 do begin avg := (DataPts[i-1] + DataPts[i] + DataPts[i+1]) / 3.0; OutPts[i] := avg; end; if j < Reps then for i := 0 to N-1 do DataPts[i] := OutPts[i]; end; // Create a new variable and copy smoothed data into it. strvalue := 'Smoothed'; col := NoVariables + 1; DictionaryFrm.NewVar(NoVariables+1); DictionaryFrm.DictGrid.Cells[1,NoVariables] := strvalue; OS3MainFrm.DataGrid.Cells[NoVariables,0] := strvalue; for i := 0 to N-1 do OS3MainFrm.DataGrid.Cells[col,i+1] := FloatToStr(OutPts[i]); end; procedure TSmoothDataForm.OutBtnClick(Sender: TObject); begin VarList.Items.Add(VariableEdit.Text); VariableEdit.Text := ''; OutBtn.Visible := false; InBtn.Visible := true; end; initialization {$I datasmoothunit.lrs} end.