File indexing completed on 2024-05-19 15:01:44
0001 /*************************************************************************** 0002 File : nsl_conv.h 0003 Project : LabPlot 0004 Description : NSL discrete convolution functions 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2018 by Stefan Gerlach (stefan.gerlach@uni.kn) 0007 0008 ***************************************************************************/ 0009 0010 /*************************************************************************** 0011 * * 0012 * This program is free software; you can redistribute it and/or modify * 0013 * it under the terms of the GNU General Public License as published by * 0014 * the Free Software Foundation; either version 2 of the License, or * 0015 * (at your option) any later version. * 0016 * * 0017 * This program is distributed in the hope that it will be useful, * 0018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0020 * GNU General Public License for more details. * 0021 * * 0022 * You should have received a copy of the GNU General Public License * 0023 * along with this program; if not, write to the Free Software * 0024 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0025 * Boston, MA 02110-1301 USA * 0026 * * 0027 ***************************************************************************/ 0028 0029 #ifndef NSL_CONV_H 0030 #define NSL_CONV_H 0031 0032 #include <stdlib.h> 0033 0034 /* when to switch from direct to FFT method */ 0035 /* set to zero to use FFT method for any length */ 0036 #define NSL_CONV_METHOD_BORDER 100 0037 0038 #define NSL_CONV_DIRECTION_COUNT 2 0039 /* forward: convolution, backward: deconvolution */ 0040 typedef enum {nsl_conv_direction_forward, nsl_conv_direction_backward} nsl_conv_direction_type; 0041 extern const char* nsl_conv_direction_name[]; 0042 0043 #define NSL_CONV_TYPE_COUNT 2 0044 /* linear (zero-padded), circular */ 0045 typedef enum {nsl_conv_type_linear, nsl_conv_type_circular} nsl_conv_type_type; 0046 extern const char* nsl_conv_type_name[]; 0047 0048 #define NSL_CONV_METHOD_COUNT 3 0049 /* auto: use direct method for small data size (NSL_CONV_METHOD_BORDER) and FFT method otherwise */ 0050 typedef enum {nsl_conv_method_auto, nsl_conv_method_direct, nsl_conv_method_fft} nsl_conv_method_type; 0051 extern const char* nsl_conv_method_name[]; 0052 0053 #define NSL_CONV_NORM_COUNT 3 0054 /* how to normalize response */ 0055 typedef enum {nsl_conv_norm_none, nsl_conv_norm_sum, nsl_conv_norm_euclidean} nsl_conv_norm_type; 0056 extern const char* nsl_conv_norm_name[]; 0057 0058 #define NSL_CONV_WRAP_COUNT 3 0059 /* how to wrap response */ 0060 typedef enum {nsl_conv_wrap_none, nsl_conv_wrap_max, nsl_conv_wrap_center} nsl_conv_wrap_type; 0061 extern const char* nsl_conv_wrap_name[]; 0062 0063 /* TODO: mode: full, same, valid (see NumPy, SciPy) */ 0064 0065 #define NSL_CONV_KERNEL_COUNT 10 0066 /* standard kernel (response) 0067 * option: number of points 0068 */ 0069 typedef enum {nsl_conv_kernel_avg, nsl_conv_kernel_smooth_triangle, nsl_conv_kernel_smooth_gaussian, nsl_conv_kernel_first_derivative, nsl_conv_kernel_smooth_first_derivative, 0070 nsl_conv_kernel_second_derivative, nsl_conv_kernel_third_derivative, nsl_conv_kernel_fourth_derivative, nsl_conv_kernel_gaussian, nsl_conv_kernel_lorentzian} nsl_conv_kernel_type; 0071 extern const char* nsl_conv_kernel_name[]; 0072 0073 /* standard kernel */ 0074 int nsl_conv_standard_kernel(double k[], size_t n, nsl_conv_kernel_type); 0075 0076 /* calculate convolution/deconvolution 0077 * of signal s of size n with response r of size m 0078 */ 0079 int nsl_conv_convolution_direction(double s[], size_t n, double r[], size_t m, nsl_conv_direction_type, nsl_conv_type_type, nsl_conv_method_type, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0080 0081 int nsl_conv_convolution(double s[], size_t n, double r[], size_t m, nsl_conv_type_type, nsl_conv_method_type, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0082 /* deconvolution only supported by FFT method */ 0083 int nsl_conv_deconvolution(double s[], size_t n, double r[], size_t m, nsl_conv_type_type, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0084 0085 /* linear/circular convolution using direct method 0086 * s and r are untouched 0087 */ 0088 int nsl_conv_linear_direct(double s[], size_t n, double r[], size_t m, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0089 int nsl_conv_circular_direct(double s[], size_t n, double r[], size_t m, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0090 /* linear/circular convolution/deconvolution using FFT method 0091 * s and r are untouched 0092 */ 0093 int nsl_conv_fft_type(double s[], size_t n, double r[], size_t m, nsl_conv_direction_type, nsl_conv_type_type, nsl_conv_norm_type normalize, nsl_conv_wrap_type wrap, double out[]); 0094 /* actual FFT method calculation using zero-padded arrays 0095 * uses FFTW if available and GSL otherwise 0096 * wi is the wrap index 0097 * s and r are overwritten 0098 */ 0099 #ifdef HAVE_FFTW3 0100 int nsl_conv_fft_FFTW(double s[], double r[], size_t n, nsl_conv_direction_type, size_t wi, double out[]); 0101 #endif 0102 int nsl_conv_fft_GSL(double s[], double r[], size_t n, nsl_conv_direction_type, double out[]); 0103 0104 #endif /* NSL_CONV_H */