Difference between revisions of "File:FourierExampleGauss04Ta.png"

From TORI
Jump to: navigation, search
(Importing image file)
 
 
Line 1: Line 1:
  +
Illustration of the numerical implementation of the [[Fourier transform]] at the sample of 4 nodes.
Importing image file
 
  +
  +
The Gaussian function $f(x)=\exp(-x^2/2)$ is shown with dashed curve.
  +
  +
The discrete analogy $A$ is shown with blue segmented line.
  +
  +
The numerical Fourier transform $B$ is shown with red segmented line.
  +
  +
The initial Gaussian is [[self-Fourier function]], and the 3-segment lines approximate it, as well as the smooth curve can be characterized at the 4-node grid.
  +
  +
The discretization of the initial Gaussian is not exact "self-Fourier":
  +
at zero, the initial approximation is few percent higher, than its Fourier transform, and for few percent lower in other three points. (The discrete Fourier reproduces the conservation of norm by the [[Fourier operator]].)
  +
  +
The colleagues are invited to load the generators below, to verify, that they reproduce the image shown, and to play the code, increasing the number N of the grid points (N should be $2^m$ for some [[natural number]] $m$) and observing how quickly the quality of the approximation improves with the increase of N.
  +
  +
==[[C++]] generator of curves==
  +
// FIles [[fafo.cin]] and [[ado.cin]] should be in the working directory for the compilation of the code below.
  +
  +
#include<math.h>
  +
#include<stdio.h>
  +
#include <stdlib.h>
  +
#include <complex>
  +
using namespace std;
  +
#define z_type complex<double>
  +
#define Re(x) x.real()
  +
#define Im(x) x.imag()
  +
#define RI(x) x.real(),x.imag()
  +
#define DB double
  +
#define DO(x,y) for(x=0;x<y;x++)
  +
  +
#include "ado.cin"
  +
#include"fafo.cin"
  +
  +
// DB F(DB x){DB u=x*x; return u*(-3.+u)*exp(-x*x/2.);} //Another self-Fourier function
  +
  +
DB F(DB x){return exp(-x*x/2.);}
  +
  +
main(){ z_type * a, *b, c; int j,m,n, N=4; FILE *o;
  +
double step=sqrt(2*M_PI/N),x,y,u;
  +
a=(z_type *) malloc((size_t)((N+1)*sizeof(z_type)));
  +
b=(z_type *) malloc((size_t)((N+1)*sizeof(z_type)));
  +
for(j=0;j<N;j++) { x=step*(j-N/2); u=x*x; a[j]=b[j]=F(x); }
  +
fafo(b,N,1);
  +
for(j=0;j<N;j++) printf("%2d %18.15f %18.15f %18.15f %18.15f\n", j, RI(a[j]), RI(b[j]) );
  +
o=fopen("FourierExampleGauss04a.eps","w"); ado(o,624,124);
  +
#define M(x,y) fprintf(o,"%6.4f %6.4f M\n",0.+x,0.+y);
  +
#define L(x,y) fprintf(o,"%6.4f %6.4f L\n",0.+x,0.+y);
  +
fprintf(o,"322 22 translate 100 100 scale\n");
  +
M(-3,0) L(3,0) M(0,0) L(0,1) fprintf(o,".01 W S\n");
  +
M(-3,1) L(3,1)
  +
for(m=-3;m<4;m++) {M(m,0) L(m,1)} fprintf(o,".004 W S\n");
  +
DO(m,121){x=-3.+.05*m; y=F(x); if(m/2*2==m)M(x,y)else L(x,y);} fprintf(o,".008 W 0 0 0 RGB S\n");
  +
DB *X; X=(DB *) malloc((size_t)((N+1)*sizeof(DB))); DO(j,N){ x=step*(j-N/2); X[j]=x; }
  +
DO(j,N){x=X[j];y=Re(a[j]); if(j==0)M(x,y)else L(x,y);} fprintf(o,".01 W 0 0 1 RGB S\n");
  +
DO(j,N){x=X[j];y=Re(b[j]); if(j==0)M(x,y)else L(x,y);} fprintf(o,"0.01 W 1 0 0 RGB S\n");
  +
// DO(j,N){x=X[j];y=100.*(Re(b[j])-F(x)); if(j==0)M(x,y)else L(x,y);} fprintf(o,"0.007 W 0 0 .3 RGB S\n");
  +
printf("X[0]=%9.5f\n",X[0]);
  +
fprintf(o,"showpage\n%cTrailer",'%'); fclose(o);
  +
system("epstopdf FourierExampleGauss04a.eps");
  +
system( "open FourierExampleGauss04a.pdf"); //these 2 commands may be specific for macintosh
  +
getchar(); system("killall Preview");// if run at another operational system, may need to modify
  +
free(a);
  +
free(b);
  +
free(X);
  +
}
  +
/*Copyeft 2011 by Dmitrii Kouznetsov */
  +
  +
==[[Latex]] generator of labels==
  +
<nowiki>
  +
\documentclass[12pt]{article} %<br>
  +
\usepackage{geometry} %<br>
  +
\paperwidth 620pt %<br>
  +
\paperheight 128pt %<br>
  +
\topmargin -99pt %<br>
  +
\oddsidemargin -84pt %<br>
  +
\parindent 0pt %<br>
  +
\pagestyle{empty} %<br>
  +
\usepackage{graphicx} %<br>
  +
\newcommand \sx \scalebox %<br>
  +
\begin{document} %<br>
  +
\begin{picture}(1024,120) %<br>
  +
\put(4,0){\includegraphics{FourierExampleGauss04a}} %<br>
  +
\put(12,114){\sx{2}{$1$}} %<br>
  +
\put(12, 16){\sx{2}{0}} %<br>
  +
%\put(0,20){\sx{2}{$-\!1$}} %<br>
  +
\put(104,3){\sx{2}{$-2$}} %<br>
  +
\put(204,3){\sx{2}{$-1$}} %<br>
  +
\put(323,3){\sx{2}{$0$}} %<br>
  +
\put(423,3){\sx{2}{$1$}} %<br>
  +
\put(523,3){\sx{2}{$2$}} %<br>
  +
\put(619,3){\sx{2}{$x$}} %<br>
  +
\end{picture} %<br>
  +
\end{document} %<br>
  +
% Copyleft 2011 by Dmitrii Kouznetsov
  +
</nowiki>
  +
  +
  +
[[Category:Fourier transform]]
  +
[[Category:Explicit plot]]
  +
[[Category:Approximations]]

Latest revision as of 09:39, 21 June 2013

Illustration of the numerical implementation of the Fourier transform at the sample of 4 nodes.

The Gaussian function $f(x)=\exp(-x^2/2)$ is shown with dashed curve.

The discrete analogy $A$ is shown with blue segmented line.

The numerical Fourier transform $B$ is shown with red segmented line.

The initial Gaussian is self-Fourier function, and the 3-segment lines approximate it, as well as the smooth curve can be characterized at the 4-node grid.

The discretization of the initial Gaussian is not exact "self-Fourier": at zero, the initial approximation is few percent higher, than its Fourier transform, and for few percent lower in other three points. (The discrete Fourier reproduces the conservation of norm by the Fourier operator.)

The colleagues are invited to load the generators below, to verify, that they reproduce the image shown, and to play the code, increasing the number N of the grid points (N should be $2^m$ for some natural number $m$) and observing how quickly the quality of the approximation improves with the increase of N.

C++ generator of curves

// FIles fafo.cin and ado.cin should be in the working directory for the compilation of the code below.

#include<math.h>
#include<stdio.h>
#include <stdlib.h>
#include <complex>
using namespace std;
#define z_type complex<double>
#define Re(x)  x.real()
#define Im(x)  x.imag()
#define RI(x)  x.real(),x.imag()
#define DB double
#define DO(x,y) for(x=0;x<y;x++)
#include "ado.cin"
#include"fafo.cin"
// DB F(DB x){DB u=x*x; return u*(-3.+u)*exp(-x*x/2.);} //Another self-Fourier function
DB F(DB x){return exp(-x*x/2.);}
main(){        z_type * a, *b, c; int j,m,n, N=4; FILE *o;
       double step=sqrt(2*M_PI/N),x,y,u;
       a=(z_type *) malloc((size_t)((N+1)*sizeof(z_type)));
       b=(z_type *) malloc((size_t)((N+1)*sizeof(z_type)));
for(j=0;j<N;j++) { x=step*(j-N/2); u=x*x; a[j]=b[j]=F(x); }
fafo(b,N,1);
for(j=0;j<N;j++) printf("%2d %18.15f %18.15f  %18.15f %18.15f\n", j, RI(a[j]), RI(b[j])  );
o=fopen("FourierExampleGauss04a.eps","w"); ado(o,624,124); 
#define M(x,y) fprintf(o,"%6.4f %6.4f M\n",0.+x,0.+y);
#define L(x,y) fprintf(o,"%6.4f %6.4f L\n",0.+x,0.+y);
fprintf(o,"322 22 translate 100 100 scale\n");
M(-3,0) L(3,0) M(0,0) L(0,1) fprintf(o,".01 W S\n");
M(-3,1) L(3,1) 
for(m=-3;m<4;m++) {M(m,0) L(m,1)} fprintf(o,".004 W S\n");
DO(m,121){x=-3.+.05*m; y=F(x); if(m/2*2==m)M(x,y)else L(x,y);} fprintf(o,".008 W 0 0 0 RGB S\n");
DB *X; X=(DB *) malloc((size_t)((N+1)*sizeof(DB))); DO(j,N){ x=step*(j-N/2); X[j]=x; }
DO(j,N){x=X[j];y=Re(a[j]); if(j==0)M(x,y)else L(x,y);} fprintf(o,".01 W 0 0 1 RGB S\n");
DO(j,N){x=X[j];y=Re(b[j]); if(j==0)M(x,y)else L(x,y);} fprintf(o,"0.01 W 1 0 0 RGB S\n");
// DO(j,N){x=X[j];y=100.*(Re(b[j])-F(x)); if(j==0)M(x,y)else L(x,y);} fprintf(o,"0.007 W 0 0 .3 RGB S\n");
printf("X[0]=%9.5f\n",X[0]);
fprintf(o,"showpage\n%cTrailer",'%'); fclose(o);
system("epstopdf FourierExampleGauss04a.eps");
system(    "open FourierExampleGauss04a.pdf"); //these 2 commands may be specific for macintosh
getchar(); system("killall Preview");// if run at another operational system, may need to modify
free(a);
free(b);
free(X);
}
/*Copyeft 2011 by Dmitrii Kouznetsov */

Latex generator of labels

\documentclass[12pt]{article} %<br> \usepackage{geometry} %<br> \paperwidth 620pt %<br> \paperheight 128pt %<br> \topmargin -99pt %<br> \oddsidemargin -84pt %<br> \parindent 0pt %<br> \pagestyle{empty} %<br> \usepackage{graphicx} %<br> \newcommand \sx \scalebox %<br> \begin{document} %<br> \begin{picture}(1024,120) %<br> \put(4,0){\includegraphics{FourierExampleGauss04a}} %<br> \put(12,114){\sx{2}{$1$}} %<br> \put(12, 16){\sx{2}{0}} %<br> %\put(0,20){\sx{2}{$-\!1$}} %<br> \put(104,3){\sx{2}{$-2$}} %<br> \put(204,3){\sx{2}{$-1$}} %<br> \put(323,3){\sx{2}{$0$}} %<br> \put(423,3){\sx{2}{$1$}} %<br> \put(523,3){\sx{2}{$2$}} %<br> \put(619,3){\sx{2}{$x$}} %<br> \end{picture} %<br> \end{document} %<br> % Copyleft 2011 by Dmitrii Kouznetsov

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current17:50, 20 June 2013Thumbnail for version as of 17:50, 20 June 20131,287 × 266 (35 KB)Maintenance script (talk | contribs)Importing image file
  • You cannot overwrite this file.

The following page links to this file:

Metadata