File indexing completed on 2024-05-12 15:27:09
0001 /*************************************************************************** 0002 File : nsl_sf_window.c 0003 Project : LabPlot 0004 Description : NSL special window functions 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2016-2017 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 #include "nsl_sf_window.h" 0030 #include "nsl_common.h" 0031 #include <gsl/gsl_math.h> 0032 #include <gsl/gsl_sf_trig.h> 0033 0034 const char* nsl_sf_window_type_name[] = {i18n("rectangular (uniform)"), i18n("triangular"), i18n("triangular II (Bartlett)"), i18n("triangular III (Parzen)") , 0035 i18n("Welch (parabolic)"), i18n("Hann (raised cosine)"), i18n("Hamming"), i18n("Blackman"), i18n("Nuttall"), i18n("Blackman-Nuttall"), i18n("Blackman-Harris"), 0036 i18n("Flat top"), i18n("Cosine"), i18n("Bartlett-Hann"), i18n("Lanczos")}; 0037 0038 int nsl_sf_apply_window(double data[], size_t N, nsl_sf_window_type type) { 0039 if (N == 0) 0040 return -1; 0041 0042 size_t i; 0043 switch (type) { 0044 case nsl_sf_window_uniform: 0045 /* do nothing */ 0046 break; 0047 case nsl_sf_window_triangle: 0048 for (i = 0; i < N; i++) 0049 data[i] = 1.0 - 2./N*fabs(i-(N-1)/2.); 0050 break; 0051 case nsl_sf_window_triangleII: 0052 for (i = 0; i < N; i++) 0053 data[i] = 1.0 - 2./(N-1)*fabs(i-(N-1)/2.); 0054 break; 0055 case nsl_sf_window_triangleIII: 0056 for (i = 0; i < N; i++) 0057 data[i] = 1.0 - 2./(N+1)*fabs(i-(N-1)/2.); 0058 break; 0059 case nsl_sf_window_welch: 0060 for (i = 0; i < N; i++) 0061 data[i] = 1.0 - gsl_pow_2(2*(i-(N-1)/2.)/(N+1)); 0062 break; 0063 case nsl_sf_window_hann: 0064 for (i = 0; i < N; i++) 0065 data[i] = 0.5*(1. - cos(2.*M_PI*i/(N-1))); 0066 break; 0067 case nsl_sf_window_hamming: 0068 for (i = 0; i < N; i++) 0069 data[i] = 0.54 - 0.46*cos(2.*M_PI*i/(N-1)); 0070 break; 0071 case nsl_sf_window_blackman: 0072 for (i = 0; i < N; i++) 0073 data[i] = 0.42 - 0.5*cos(2.*M_PI*i/(N-1)) + 0.08*cos(4.*M_PI*i/(N-1)); 0074 break; 0075 case nsl_sf_window_nuttall: 0076 for (i = 0; i < N; i++) 0077 data[i] = 0.355768 - 0.487396*cos(2.*M_PI*i/(N-1)) + 0.144232*cos(4.*M_PI*i/(N-1)) - 0.012604*cos(6.*M_PI*i/(N-1)); 0078 break; 0079 case nsl_sf_window_blackman_nuttall: 0080 for (i = 0; i < N; i++) 0081 data[i] = 0.3635819 - 0.4891775*cos(2.*M_PI*i/(N-1)) + 0.1365995*cos(4.*M_PI*i/(N-1)) - 0.0106411*cos(6.*M_PI*i/(N-1)); 0082 break; 0083 case nsl_sf_window_blackman_harris: 0084 for (i = 0; i < N; i++) 0085 data[i] = 0.35875 - 0.48829*cos(2.*M_PI*i/(N-1)) + 0.14128*cos(4.*M_PI*i/(N-1)) - 0.01168*cos(6.*M_PI*i/(N-1)); 0086 break; 0087 case nsl_sf_window_flat_top: 0088 for (i = 0; i < N; i++) 0089 data[i] = 1 - 1.93*cos(2.*M_PI*i/(N-1)) + 1.29*cos(4.*M_PI*i/(N-1)) - 0.388*cos(6.*M_PI*i/(N-1)) + 0.028*cos(8.*M_PI*i/(N-1)); 0090 break; 0091 case nsl_sf_window_cosine: 0092 for (i = 0; i < N; i++) 0093 data[i] = sin(M_PI*i/(N-1)); 0094 break; 0095 case nsl_sf_window_bartlett_hann: 0096 for (i = 0; i < N; i++) 0097 data[i] = 0.62 - 0.48*fabs(i/(double)(N-1)-0.5) - 0.38*cos(2.*M_PI*i/(N-1)); 0098 break; 0099 case nsl_sf_window_lanczos: 0100 for (i = 0; i < N; i++) 0101 data[i] = gsl_sf_sinc(2.*i/(N-1)-1.); 0102 break; 0103 } 0104 0105 return 0; 0106 }