Commit 6d348fbd authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

tests

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 2376
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent d8af581c
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id: Makefile,v 1.9 2007-09-11 07:35:27 tforb Exp $
# $Id: Makefile,v 1.10 2007-09-12 10:05:28 tforb Exp $
#
# Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
#
......@@ -21,7 +21,7 @@
all: install libfourier.doc
install: install-include libfourier.a libfourierxx.a
flist: Makefile $(wildcard *.f *.inc *.h *.cc)
flist: Makefile $(wildcard *.f *.inc *.h *.cc *.c)
echo $^ | tr ' ' '\n' | sort > $@
.PHONY: edit
......@@ -218,6 +218,16 @@ install-include: $(INSTHEADER)
echo $^ | tr ' ' '\n'
touch $@
#======================================================================
# tests
# -----
cfftwtest: cfftwtest.c
$(CC) -o $@ $< -ldrfftw -ldfftw -lm
cxxfftwtest: cxxfftwtest.cc
$(CXX) -o $@ $< -ldrfftw -ldfftw -lm
#======================================================================
# documentation part
# ------------------
......
/*! \file cfftwtest.c
* \brief a small test program for fftw (implementation)
*
* ----------------------------------------------------------------------------
*
* $Id: cfftwtest.c,v 1.1 2007-09-12 10:05:29 tforb Exp $
* \author Thomas Forbriger
* \date 12/09/2007
*
* a small test program for fftw (implementation)
*
* Copyright (c) 2007 by Thomas Forbriger (BFO Schiltach)
*
* REVISIONS and CHANGES
* - 12/09/2007 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_CFFTWTEST_C_VERSION \
"TF_CFFTWTEST_C V1.0 "
#define TF_CFFTWTEST_C_CVSID \
"$Id: cfftwtest.c,v 1.1 2007-09-12 10:05:29 tforb Exp $"
#include<drfftw.h>
#include<stdio.h>
#include<math.h>
void fill(fftw_real* a, int n, int m)
{
int k;
double f1, f2;
f1=3.141592653589/n;
f2=f1*m*2;
/* fill input array */
for (k=0; k<n; ++k)
{
a[k]=sin(k*f1)*sin(k*f1)*sin(k*f2);
}
}
/*
* the code provided in the tutorial for rfftw_one produces segmentation
* faults if used together with heap memory
*
* solution: always use drfftw.h together with double precision transforms!
*/
void process(int n, int m)
{
// create arrays
fftw_real* in=malloc(n*sizeof(fftw_real));
fftw_real* out=malloc(n*sizeof(fftw_real));
fftw_real* power_spectrum=malloc((n/2+1)*sizeof(fftw_real));
if (in == NULL) { printf("could not alloc in\n"); abort(); }
if (out == NULL) { printf("could not alloc out\n"); abort(); }
if (power_spectrum == NULL)
{ printf("could not alloc power_spectrum\n"); abort(); }
rfftw_plan p;
int k;
fill(in, n, m);
p = rfftw_create_plan(n, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
rfftw_one(p, in, out);
power_spectrum[0] = out[0]*out[0]; // DC component
for (k = 1; k < (n+1)/2; ++k) // (k < N/2 rounded up)
power_spectrum[k] = out[k]*out[k] + out[n-k]*out[n-k];
if (n % 2 == 0) // N is even
power_spectrum[n/2] = out[n/2]*out[n/2]; // Nyquist freq.
rfftw_destroy_plan(p);
printf("\nPower spectrum of %d point signal with %d periods\n", n, m);
printf("\ni.e. signal frequency %f\n", (double)m/n);
for (k=0; k<((n+1)/2); ++k)
{
printf(" %3.3d, %5f: %10e\n",k, (double)k/n, power_spectrum[k]);
}
free(in);
free(out);
free(power_spectrum);
}
#define N 2048
int main()
{
printf("%s\n", TF_CFFTWTEST_C_VERSION);
printf("%s\n", TF_CFFTWTEST_C_CVSID);
/*
* the code provided in the tutorial for rfftw_one produces segmentation
* faults if used together with stack memory for N=2000 or larger
*
* segfaults can be prevented if in and out array dimensions are set to the
* double size that is required
*
* solution: always use drfftw.h together with double precision transforms!
*/
fftw_real in[N], out[N], power_spectrum[N/2+1];
rfftw_plan p;
int k;
int m;
m=5;
fill(in, N, m);
p = rfftw_create_plan(N, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
rfftw_one(p, in, out);
power_spectrum[0] = out[0]*out[0]; // DC component
for (k = 1; k < (N+1)/2; ++k) // (k < N/2 rounded up)
power_spectrum[k] = out[k]*out[k] + out[N-k]*out[N-k];
if (N % 2 == 0) // N is even
power_spectrum[N/2] = out[N/2]*out[N/2]; // Nyquist freq.
rfftw_destroy_plan(p);
printf("\nPower spectrum of %d point signal with %d periods\n", N, m);
printf("\ni.e. signal frequency %f\n", (double)m/N);
for (k=0; k<((N+1)/2); ++k)
{
printf(" %3.3d, %5f: %10e\n",k, (double)k/N, power_spectrum[k]);
}
fill(in, N, m);
p = rfftw_create_plan(N, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
rfftw_one(p, in, out);
power_spectrum[0] = out[0]*out[0]; // DC component
for (k = 1; k < (N+1)/2; ++k) // (k < N/2 rounded up)
power_spectrum[k] = out[k]*out[k] + out[N-k]*out[N-k];
if (N % 2 == 0) // N is even
power_spectrum[N/2] = out[N/2]*out[N/2]; // Nyquist freq.
rfftw_destroy_plan(p);
printf("\nPower spectrum of %d point signal with %d periods\n", N, m);
printf("\ni.e. signal frequency %f\n", (double)m/N);
for (k=0; k<((N+1)/2); ++k)
{
printf(" %3.3d, %5f: %10e\n",k, (double)k/N, power_spectrum[k]);
}
process(200, 20);
process(50000, 2000);
}
/* ----- END OF cfftwtest.c ----- */
/*! \file cxxfftwtest.cc
* \brief a small test program for fftw when called from C++
*
* ----------------------------------------------------------------------------
*
* $Id: cxxfftwtest.cc,v 1.1 2007-09-12 10:05:29 tforb Exp $
* \author Thomas Forbriger
* \date 12/09/2007
*
* a small test program for fftw when called from C++
*
* Copyright (c) 2007 by Thomas Forbriger (BFO Schiltach)
*
* REVISIONS and CHANGES
* - 12/09/2007 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define CXXFFTWTEST_VERSION \
"CXXFFTWTEST V1.0 a small test program for fftw when called from C++"
#define CXXFFTWTEST_CVSID \
"$Id: cxxfftwtest.cc,v 1.1 2007-09-12 10:05:29 tforb Exp $"
#include <iostream>
#include <tfxx/commandline.h>
using std::cout;
using std::cerr;
using std::endl;
int main(int iargc, char* argv[])
{
// define usage information
char usage_text[]=
{
CXXFFTWTEST_VERSION "\n"
"usage: cxxfftwtest" "\n"
" or: cxxfftwtest --help|-h" "\n"
};
// define full help text
char help_text[]=
{
CXXFFTWTEST_CVSID
};
// define commandline options
using namespace tfxx::cmdline;
static Declare options[]=
{
// 0: print help
{"help",arg_no,"-"},
// 1: verbose mode
{"v",arg_no,"-"},
{NULL}
};
// no arguments? print usage...
if (iargc<2)
{
cerr << usage_text << endl;
exit(0);
}
// collect options from commandline
Commandline cmdline(iargc, argv, options);
// help requested? print full help text...
if (cmdline.optset(0))
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
// dummy operation: print option settings
for (int iopt=0; iopt<2; iopt++)
{
cout << "option: '" << options[iopt].opt_string << "'" << endl;
if (cmdline.optset(iopt)) { cout << " option was set"; }
else { cout << "option was not set"; }
cout << endl;
cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
cout << " argument (bool): '";
if (cmdline.bool_arg(iopt))
{ cout << "true"; } else { cout << "false"; }
cout << "'" << endl;
}
while (cmdline.extra()) { cout << cmdline.next() << endl; }
// dummy operation: print rest of command line
while (cmdline.extra()) { cout << cmdline.next() << endl; }
}
/* ----- END OF cxxfftwtest.cc ----- */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment