Help with Fourier - page 2

 
Amplitude and phase are ONE complex number, 1 bar is also 1 number in which the imaginary part = 0. Therefore 8 bars is 8 frequencies. Let's ask klot what he thinks about this?
 
Something's not sending the file
I'll try again.
Files:
 
Anyway, that's better :)

//+------------------------------------------------------------------+
//|                                            #_i_SpecktrAnalis.mq4 |
//|                                          Copyright © 2006, klot. |
//|                                                     klot@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, klot."
#property link      "klot@mail.ru"
//---
#include <stdlib.mqh>
#define pi 3.14159265358979323846
//---
#import "#_lib_FFT.ex4"
void realfastfouriertransform(double& a[], int tnn, bool inversefft);
#import
//---
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
double SpecktrBuffer[];
//---
extern double n=8;// Задает размер массива - степень двойки
extern double f=1.0;// Частота периодической функции
extern double ff=0;// Фаза периодической функции
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,SpecktrBuffer);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int tnn1=MathPow(2,n);// По ограничению функции- размер массива длжен быть степенью двойки
   double aa[];
   int N=ArrayResize(aa,tnn1);
   SetIndexDrawBegin(0,Bars-N);
   SetIndexDrawBegin(1,Bars-N);
   //ArrayResize(aa,tnn1+1); //Для косинус-преобразования
   //---
   // Исследование спектра разных функций
   double sig;
   for(int i=0; i<=N-1; i++)
   {
      sig=MathCos(f*i/(2.0*pi)+ff*pi); // Обыкновенная периодическая функция - к рынку не имееет отношения
      //sig=Close[i];
      //sig=iRSI(NULL,0,14,PRICE_CLOSE,i+1);
      aa[i]=sig;
      
   }
   //InSigNormalize(aa); //Нормализация значений 
   // Прямое преобразование Фурье - после выпонения функции в массиве aa[] - спектрограмма
   realfastfouriertransform(aa, tnn1, false); 
   InSigNormalize(aa); //Нормализация значений 
   
   //--- Вывод спектрограммы на экран
   for( i=0; i<=N-1; i++)
   {
      // Модуль комплексного числа
      SpecktrBuffer[i]=MathSqrt(aa[i*2]*aa[i*2]+aa[i*2+1]*aa[i*2+1]); 
   }
   //---
   /*
   //realfastfouriertransform(aa, tnn1, true);
   for( i=0; i<=N; i++)
   {
      SpecktrBuffer[i]=aa[i];
   }*/
 
   //----
   return(0);
  }
  
//+------------------------------------------------------------------+
 
//--------------------------------------------------------------------+
void InSigNormalize(double& aa[])
{
   double sum_sqrt;
   int element_count=ArraySize(aa);
   sum_sqrt=0;
   for( int i=0; i<=element_count-1; i++)
   {
      sum_sqrt+=MathPow(aa[i],2);
   }
   sum_sqrt=MathSqrt(sum_sqrt);
   
   if (sum_sqrt!=0)
   {
      for( i=0; i<=element_count-1; i++)
      {
         aa[i]=aa[i]/sum_sqrt;
      }
   }
   return;
}
//---------------------------------------------------------------------+
 
lsv писал (а):
Amplitude and phase are ONE complex number, 1 bar is also 1 number in which the imaginary part = 0. Therefore 8 bars is 8 frequencies. Let's ask klot what he thinks about it.

I did it with complex numbers:
//--- Output the spectrogram to the screen
for( i=0; i<=N-1; i++)
{
// Module of a complex number
SpecktrBuffer[i]=MathSqrt(aa[i*2]*aa[i*2]+aa[i*2+1]*aa[i*2+1]);
}
//---
 
lsv:
Amplitude and phase are ONE complex number, 1 bar is also 1 number in which the imaginary part = 0. Therefore 8 bars is 8 frequencies. Let's ask klot what he thinks about it?
You'd better ask me :)
I've been dealing with this subject for several years :)
there's even a bunch of pages about TF on my website
http://www.may.nnov.ru/mak/DSP/Contents.shtml

Or google it.
Here's the first link, for example.
http://alglib.sources.ru/fft/realfft.php

end of page:

If we compare this picture with a similar one for the FFT complex function, we notice that frequencies f-1 to f-N/2+1 disappeared somewhere, and only real parts remain from frequencies f0and fN/2, taking the former place of the complex frequency f0. The reason for this is the symmetry properties of the Fourier transform: for the real function h(t) it is true that H(-f) = H *(f).

Thus, the frequencies f-1 to f-N/2+1 no longer carry any new information, as they are obtained by complex conjugation of their symmetric twins, and the frequencies f0and fN/2 have imaginary parts equal to zero.

===========================================================
A complex number contains 2 independent components,
A complex number with an imaginary part equal to zero contains 1 independent component.

If the input is a series of complex numbers, we get K frequencies,
If the input is a number of real numbers, then half of the frequencies remain.
 
My reasoning is simpler: There is a theorem, but I can't remember the last name of the person who proved it: to convert an analogue signal to digital, it takes twice as many samples as the stored frequency to store a certain frequency. In our case, there are 8 samples, so they can store frequency information for no more than 4 periods.
 
Kotelnikov's theorem.
 

Judging by the discussion, this Fourier is something interesting, but unfortunately I don't understand what it's all about,
Can someone explain in a nutshell what it is and how to make an EA out of it?

 
Ronen:

Judging by the discussion, this Fourier is something interesting, but unfortunately I do not understand what we're talking about here,
can someone explain in a nutshell what it is and how to make an EA out of it?


I can make some inaccuracies.... The point is that any periodic function can be represented as a sum of sinusoidal components of different frequencies, i.e. it can be expanded into Fourier series. Fourier series is a sum of sine and cosine components of doubling frequency (harmonics). By some mathematical manipulations with Fourier transforms data series may be presented as sum ofuniformly varying frequency sinusoids and for each component we obtain amplitude, simply speaking, we obtain frequency response of signal (amplitude of sinusoids with frequency of 1 Hz, 2, 3, etc.). Then by manipulating each frequency component it is possible to filter the signal, etc. etc. That's OK, but one thing, the function has to be periodic, even if it's not periodic, these perobranches imply that it's periodic. Although who knows... maybe there's some benefit to it.
 
Integer писал (а):
The point is that any periodic function can be represented as ...

Well, if it's not too difficult - a little more for the dummie's level:

Maybe I'm wrong, but it seems to me that small TFs fit rather well to this condition. I observe it now, passing values from M1 with prevBars!=Bars by global variables and reading them at M15 and H1 with 1 min. interval, I see - whether it may strongly affect an entry point inside a bar of the major TF. Maybe for fun, or maybe - "Inside each hourly bar sits 10p of profit :). Or 15p loss :(." If the sine/cosine really does lie - for intraday it will be a real add-on. Seems to me.
Reason: