From 79a083f28e9df04768151c8a916934716b4a2646 Mon Sep 17 00:00:00 2001 From: Richard Knight Date: Mon, 24 Aug 2020 05:20:46 +0100 Subject: initial --- src/cosine.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/cosine.cpp (limited to 'src/cosine.cpp') diff --git a/src/cosine.cpp b/src/cosine.cpp new file mode 100644 index 0000000..78cafd8 --- /dev/null +++ b/src/cosine.cpp @@ -0,0 +1,131 @@ +/* + cosine.cpp + Copyright (C) 2015 John Burkardt + Copyright (C) 2019 Richard Knight + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + */ + +#include +#include +#include +#include +#include + +using namespace std; + +#include "cosine.hpp" + +//****************************************************************************80 + +void cosine_transform_data (csnd::Vector d, csnd::Vector c) + +//****************************************************************************80 +// +// Purpose: +// +// COSINE_TRANSFORM_DATA does a cosine transform on a vector of data. +// +// Licensing: +// +// This code is distributed under the GNU LGPL license. +// +// Modified: +// +// 27 August 2015 +// +// Author: +// +// John Burkardt +// +// Parameters: +// +// Input, integer N, the number of data points. +// +// Input, double D[N], the vector of data. +// +// Output, double COSINE_TRANSFORM_DATA[N], the transform coefficients. +// +{ + int n = d.len(); + double angle; + int i; + int j; + const double r8_pi = 3.141592653589793; + + + for ( i = 0; i < n; i++ ) + { + c[i] = 0.0; + for ( j = 0; j < n; j++ ) + { + angle = r8_pi * ( double ) ( i * ( 2 * j + 1 ) ) / ( double ) ( 2 * n ); + c[i] = c[i] + cos ( angle ) * d[j]; + } + c[i] = c[i] * sqrt ( 2.0 / ( double ) ( n ) ); + } +} +//****************************************************************************80 + +void cosine_transform_inverse (csnd::Vector c, csnd::Vector d) + +//****************************************************************************80 +// +// Purpose: +// +// COSINE_TRANSFORM_INVERSE does an inverse cosine transform. +// +// Licensing: +// +// This code is distributed under the GNU LGPL license. +// +// Modified: +// +// 27 August 2015 +// +// Author: +// +// John Burkardt +// +// Parameters: +// +// Input, integer N, the number of data points. +// +// Input, double C[N], the vector of transform coefficients +// +// Output, double COSINE_TRANSFORM_INVERSE[N], the original data. +// +{ + int n = c.len(); + double angle; + int i; + int j; + double r8_pi = 3.141592653589793; + + + for ( i = 0; i < n; i++ ) + { + d[i] = c[0] / 2.0; + for ( j = 1; j < n; j++ ) + { + angle = r8_pi * ( double ) ( ( 2 * i + 1 ) * j ) / ( double ) ( 2 * n ); + d[i] = d[i] + cos ( angle ) * c[j]; + } + d[i] = d[i] * sqrt ( 2.0 / ( double ) ( n ) ); + } +} +//****************************************************************************80 -- cgit v1.2.3