unit UInspectTFDFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, StimuliClass, Series,
  StdCtrls, Buttons;

type
  TInspectTFDFrameForm = class(TForm)
    Splitter: TSplitter;
    modulePanel: TPanel;
    moduleChart: TChart;
    Series1: TLineSeries;
    Panel2: TPanel;
    Shape1: TShape;
    Shape2: TShape;
    GraphOptionsModuleButton: TButton;
    zoomModeRB: TRadioButton;
    CursorModeRB: TRadioButton;
    phasePanel: TPanel;
    phaseChart: TChart;
    Series2: TLineSeries;
    Panel4: TPanel;
    Shape3: TShape;
    Shape4: TShape;
    phaseVisibleSpeedButton: TSpeedButton;
    phaseInvivsibleSpeedButton: TSpeedButton;
    GraphOptionsPhaseButton: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure GraphOptionsModuleButtonClick(Sender: TObject);
    procedure GraphOptionsPhaseButtonClick(Sender: TObject);
    procedure phaseVisibleSpeedButtonClick(Sender: TObject);
    procedure phaseInvivsibleSpeedButtonClick(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    constructor Create(AOwner: TComponent; AStimulus : TStimulus; seuilModule : double);
  end;

var
  InspectTFDFrameForm: TInspectTFDFrameForm;

implementation

uses {UDFT,} UGrapheOptionsInspect, Complexs, FFTs;

{$R *.dfm}

constructor TInspectTFDFrameForm.Create(AOwner: TComponent; AStimulus : TStimulus; seuilModule : double);
var x : array of TComplex;
    fftx : array of TComplex;
    i : integer;
    nbPoints : integer ;
begin
  inherited Create(AOwner);
  Show;
  Caption := 'TFD de '  + AStimulus.Title;


  SetLength(x,AStimulus.Count);
  SetLength(fftx,AStimulus.Count);
  for i := 0 to AStimulus.Count - 1 do x[i] := Complex(AStimulus.YValues[i],0.0);

  ForwardFFT(x,fftx,AStimulus.Count);

  nbPoints := high(fftx) + 1;

  for i := 0 to nbPoints div 2
  do Series1.AddXY(i*AStimulus.Fe/nbPoints,ComplexMag(fftx[i])/AStimulus.Count);

  for i := ((nbPoints div 2) + 1) to nbPoints - 1
  do Series1.AddXY(-(nbPoints-i)*AStimulus.Fe/nbPoints,ComplexMag(fftx[i])/AStimulus.Count);

  for i := 0 to nbPoints div 2 do
  begin
   if(ComplexMag(fftx[i])/AStimulus.Count < seuilModule)
   then Series2.AddXY(i*AStimulus.Fe/nbPoints,0)
   else Series2.AddXY(i*AStimulus.Fe/nbPoints,ComplexPhase(fftx[i]));
  end;

  for i := ((nbPoints div 2) + 1) to nbPoints - 1 do
  begin
    if(ComplexMag(fftx[i])/AStimulus.Count < seuilModule)
    then Series2.AddXY(-(nbPoints-i)*AStimulus.Fe/nbPoints,0)
    else Series2.AddXY(-(nbPoints-i)*AStimulus.Fe/nbPoints,ComplexPhase(fftx[i]));
  end;

  SetLength(x,0);
  SetLength(fftx,0);
  
  Caption := Caption + ' - ' + inttostr(nbPoints) + ' points - df = ' + floattostr(AStimulus.Fe/nbPoints);
end;


procedure TInspectTFDFrameForm.GraphOptionsModuleButtonClick(
  Sender: TObject);
begin
  GraphOptionsInspectForm.AChart := moduleChart;
	GraphOptionsInspectForm.ShowModal;
end;

procedure TInspectTFDFrameForm.GraphOptionsPhaseButtonClick(
  Sender: TObject);
begin
  GraphOptionsInspectForm.AChart := phaseChart;
	GraphOptionsInspectForm.ShowModal;
end;

procedure TInspectTFDFrameForm.phaseVisibleSpeedButtonClick(
  Sender: TObject);
begin
  phasePanel.Visible := true;
    Splitter.Visible :=true;
end;

procedure TInspectTFDFrameForm.phaseInvivsibleSpeedButtonClick(
  Sender: TObject);
begin
  Splitter.Visible :=false;
  phasePanel.Visible := false;
end;

end.
