]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/chirp.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / chirp.m
1 ## Copyright (C) 1999-2000 Paul Kienzle <pkienzle@users.sf.net>
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 ## usage: y = chirp(t [, f0 [, t1 [, f1 [, form [, phase]]]]])
17 ##
18 ## Evaluate a chirp signal at time t.  A chirp signal is a frequency
19 ## swept cosine wave.
20 ##
21 ## t: vector of times to evaluate the chirp signal
22 ## f0: frequency at time t=0 [ 0 Hz ]
23 ## t1: time t1 [ 1 sec ]
24 ## f1: frequency at time t=t1 [ 100 Hz ]
25 ## form: shape of frequency sweep
26 ##    'linear'      f(t) = (f1-f0)*(t/t1) + f0
27 ##    'quadratic'   f(t) = (f1-f0)*(t/t1)^2 + f0
28 ##    'logarithmic' f(t) = (f1-f0)^(t/t1) + f0
29 ## phase: phase shift at t=0
30 ##
31 ## Example
32 ##    specgram(chirp([0:0.001:5])); # linear, 0-100Hz in 1 sec
33 ##    specgram(chirp([-2:0.001:15], 400, 10, 100, 'quadratic'));
34 ##    soundsc(chirp([0:1/8000:5], 200, 2, 500, "logarithmic"),8000);
35 ##
36 ## If you want a different sweep shape f(t), use the following:
37 ##    y = cos(2*pi*integral(f(t)) + 2*pi*f0*t + phase);
38
39 function y = chirp(t, f0, t1, f1, form, phase)
40
41   if nargin < 1 || nargin > 6
42     print_usage;
43   endif
44   if nargin < 2, f0 = []; endif
45   if nargin < 3, t1 = []; endif
46   if nargin < 4, f1 = []; endif
47   if nargin < 5, form = []; endif
48   if nargin < 6, phase = []; endif
49
50   if isempty(f0), f0 = 0; endif
51   if isempty(t1), t1 = 1; endif
52   if isempty(f1), f1 = 100; endif
53   if isempty(form), form = "linear"; endif
54   if isempty(phase), phase = 0; endif
55
56   phase = 2*pi*phase/360;
57
58   if strcmp(form, "linear")
59     a = pi*(f1 - f0)/t1;
60     b = 2*pi*f0;
61     y = cos(a*t.^2 + b*t + phase);
62   elseif strcmp(form, "quadratic")
63     a = (2/3*pi*(f1-f0)/t1/t1);
64     b = 2*pi*f0;
65     y = cos(a*t.^3 + b*t + phase);
66   elseif strcmp(form, "logarithmic")
67     a = 2*pi*t1/log(f1-f0);
68     b = 2*pi*f0;
69     x = (f1-f0)^(1/t1);
70     y = cos(a*x.^t + b*t + phase);
71   else
72     error("chirp doesn't understand '%s'",form);
73   endif
74
75 endfunction
76
77 %!demo
78 %! specgram(chirp([0:0.001:5]),[],1000); # linear, 0-100Hz in 1 sec
79 %! %------------------------------------------------------------
80 %! % Shows linear sweep of 100 Hz/sec starting at zero for 5 sec
81 %! % since the sample rate is 1000 Hz, this should be a diagonal
82 %! % from bottom left to top right.
83
84 %!demo
85 %! specgram(chirp([-2:0.001:15], 400, 10, 100, 'quadratic'));
86 %! %------------------------------------------------------------
87 %! % Shows a quadratic chirp of 400 Hz at t=0 and 100 Hz at t=10
88 %! % Time goes from -2 to 15 seconds.
89
90 %!demo
91 %! specgram(chirp([0:1/8000:5], 200, 2, 500, "logarithmic"),[],8000);
92 %! %------------------------------------------------------------
93 %! % Shows a logarithmic chirp of 200 Hz at t=0 and 500 Hz at t=2
94 %! % Time goes from 0 to 5 seconds at 8000 Hz.