0
|
1 ## Copyright (C) 1999-2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## usage: y = chirp(t [, f0 [, t1 [, f1 [, form [, phase]]]]]) |
|
18 ## |
|
19 ## Evaluate a chirp signal at time t. A chirp signal is a frequency |
|
20 ## swept cosine wave. |
|
21 ## |
|
22 ## t: vector of times to evaluate the chirp signal |
|
23 ## f0: frequency at time t=0 [ 0 Hz ] |
|
24 ## t1: time t1 [ 1 sec ] |
|
25 ## f1: frequency at time t=t1 [ 100 Hz ] |
|
26 ## form: shape of frequency sweep |
|
27 ## 'linear' f(t) = (f1-f0)*(t/t1) + f0 |
|
28 ## 'quadratic' f(t) = (f1-f0)*(t/t1)^2 + f0 |
|
29 ## 'logarithmic' f(t) = (f1-f0)^(t/t1) + f0 |
|
30 ## phase: phase shift at t=0 |
|
31 ## |
|
32 ## Example |
|
33 ## specgram(chirp([0:0.001:5])); # linear, 0-100Hz in 1 sec |
|
34 ## specgram(chirp([-2:0.001:15], 400, 10, 100, 'quadratic')); |
|
35 ## soundsc(chirp([0:1/8000:5], 200, 2, 500, "logarithmic"),8000); |
|
36 ## |
|
37 ## If you want a different sweep shape f(t), use the following: |
|
38 ## y = cos(2*pi*integral(f(t)) + 2*pi*f0*t + phase); |
|
39 |
|
40 ## 2001-08-31 Paul Kienzle <pkienzle@users.sf.net> |
|
41 ## * Fix documentation for quadratic case |
|
42 |
|
43 function y = chirp(t, f0, t1, f1, form, phase) |
|
44 |
|
45 if nargin < 1 || nargin > 6 |
|
46 usage("y = chirp(t [, f0 [, t1 [, f1 [, form [, phase]]]]])"); |
|
47 endif |
|
48 if nargin < 2, f0 = []; endif |
|
49 if nargin < 3, t1 = []; endif |
|
50 if nargin < 4, f1 = []; endif |
|
51 if nargin < 5, form = []; endif |
|
52 if nargin < 6, phase = []; endif |
|
53 |
|
54 if isempty(f0), f0 = 0; endif |
|
55 if isempty(t1), t1 = 1; endif |
|
56 if isempty(f1), f1 = 100; endif |
|
57 if isempty(form), form = "linear"; endif |
|
58 if isempty(phase), phase = 0; endif |
|
59 |
|
60 phase = 2*pi*phase/360; |
|
61 |
|
62 if strcmp(form, "linear") |
|
63 a = pi*(f1 - f0)/t1; |
|
64 b = 2*pi*f0; |
|
65 y = cos(a*t.^2 + b*t + phase); |
|
66 elseif strcmp(form, "quadratic") |
|
67 a = (2/3*pi*(f1-f0)/t1/t1); |
|
68 b = 2*pi*f0; |
|
69 y = cos(a*t.^3 + b*t + phase); |
|
70 elseif strcmp(form, "logarithmic") |
|
71 a = 2*pi*t1/log(f1-f0); |
|
72 b = 2*pi*f0; |
|
73 x = (f1-f0)^(1/t1); |
|
74 y = cos(a*x.^t + b*t + phase); |
|
75 else |
|
76 error("chirp doesn't understand '%s'",form); |
|
77 endif |
|
78 |
|
79 endfunction |
|
80 |
|
81 %!demo |
|
82 %! specgram(chirp([0:0.001:5]),[],1000); # linear, 0-100Hz in 1 sec |
|
83 %! %------------------------------------------------------------ |
|
84 %! % Shows linear sweep of 100 Hz/sec starting at zero for 5 sec |
|
85 %! % since the sample rate is 1000 Hz, this should be a diagonal |
|
86 %! % from bottom left to top right. |
|
87 |
|
88 %!demo |
|
89 %! specgram(chirp([-2:0.001:15], 400, 10, 100, 'quadratic')); |
|
90 %! %------------------------------------------------------------ |
|
91 %! % Shows a quadratic chirp of 400 Hz at t=0 and 100 Hz at t=10 |
|
92 %! % Time goes from -2 to 15 seconds. |
|
93 |
|
94 %!demo |
|
95 %! specgram(chirp([0:1/8000:5], 200, 2, 500, "logarithmic"),[],8000); |
|
96 %! %------------------------------------------------------------ |
|
97 %! % Shows a logarithmic chirp of 200 Hz at t=0 and 500 Hz at t=2 |
|
98 %! % Time goes from 0 to 5 seconds at 8000 Hz. |