]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/tukeywin.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / tukeywin.m
1 ## Copyright (C) 2007 Laurent Mazet <mazet@crm.mot.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{w} =} tukeywin (@var{L}, @var{r})
18 ## Return the filter coefficients of a Tukey window (also known as the
19 ## cosine-tapered window) of length @var{L}. @var{r} defines the ratio
20 ## between the constant section and and the cosine section. It has to be
21 ## between 0 and 1. The function returns a Hanning window for @var{r}
22 ## egals 0 and a full box for @var{r} egals 1. By default @var{r} is set
23 ## to 1/2.
24 ##
25 ## For a definition of the Tukey window, see e.g. Fredric J. Harris,
26 ## "On the Use of Windows for Harmonic Analysis with the Discrete Fourier
27 ## Transform, Proceedings of the IEEE", Vol. 66, No. 1, January 1978,
28 ## Page 67, Equation 38.
29 ## @end deftypefn
30
31 function w = tukeywin (L, r = 1/2)
32
33   if (nargin < 1 || nargin > 2)
34     print_usage;
35   elseif (nargin == 2)
36       ## check that 0 < r < 1
37       if r > 1
38         r = 1;
39       elseif r < 0
40         r = 0;
41       endif
42   endif
43
44   ## generate window
45   switch r
46     case 0,
47       ## full box
48       w = ones (L, 1);
49     case 1,
50       ## Hanning window
51       w = hanning (L);
52     otherwise
53       ## cosine-tapered window
54       t = linspace(0,1,L)(1:end/2)';
55       w = (1 + cos(pi*(2*t/r-1)))/2;
56       w(floor(r*(L-1)/2)+2:end) = 1;
57       w = [w; ones(mod(L,2)); flipud(w)];
58   endswitch
59
60 endfunction
61
62 %!demo
63 %! L = 100;
64 %! r = 1/3;
65 %! w = tukeywin (L, r);
66 %! title(sprintf("%d-point Tukey window, R = %d/%d", L, [p, q] = rat(r), q));
67 %! plot(w);