1%-*- texinfo -*-
2%@deftypefn {Function} demo_dgt
3%@verbatim
4%DEMO_DGT  Basic introduction to DGT analysis/synthesis
5%
6%   This demo shows how to compute Gabor coefficients of a signal.
7%
8%   Figure 1: Spectrogram of the 'bat' signal.
9%
10%      The figure shows a spectrogram of the 'bat' signal. The
11%      coefficients are shown on a linear scale.
12%
13%   Figure 2: Gabor coefficients of the 'bat' signal.
14%
15%      The figure show a set of Gabor coefficients for the 'bat' signal,
16%      computed using a DGT with a Gaussian window. The coefficients
17%      contains all the information to reconstruct the signal, even though
18%      there a far fewer coefficients than the spectrogram contains.
19%
20%   Figure 3: Real-valued Gabor analysis
21%
22%      This figure shows only the coefficients for the positive
23%      frequencies. As the signal is real-value, these coefficients
24%      contain all the necessary information. Compare to the shape of the
25%      spectrogram shown on Figure 1.
26%
27%   Figure 4: DGT coefficients on a spectrogram
28%
29%      This figure shows how the coefficients from DGTREAL can be picked
30%      from the coefficients computed by a full Short-time Fourier
31%      transform, as visualized by a spectrogram.
32%
33%@end verbatim
34%@strong{Url}: @url{http://ltfat.github.io/doc/demos/demo_dgt.html}
35%@seealso{sgram, dgt, dgtreal}
36%@end deftypefn
37
38% Copyright (C) 2005-2016 Peter L. Soendergaard <peter@sonderport.dk>.
39% This file is part of LTFAT version 2.3.1
40%
41% This program is free software: you can redistribute it and/or modify
42% it under the terms of the GNU General Public License as published by
43% the Free Software Foundation, either version 3 of the License, or
44% (at your option) any later version.
45%
46% This program is distributed in the hope that it will be useful,
47% but WITHOUT ANY WARRANTY; without even the implied warranty of
48% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49% GNU General Public License for more details.
50%
51% You should have received a copy of the GNU General Public License
52% along with this program.  If not, see <http://www.gnu.org/licenses/>.
53
54disp('Type "help demo_dgt" to see a description of how this demo works.');
55
56% Load a test signal
57f=bat;
58
59% sampling rate of the test signal, only important for plotting
60fs=143000;
61
62% Length of signal
63Ls=length(f);
64
65disp(' ');
66disp('------ Spectrogram analysis -----------------------------------');
67
68figure(1);
69c_sgram=sgram(f,fs,'lin');
70title('Spectrogram of the bat test signal.');
71
72
73% Number of coefficients in the Spectrogram
74no_sgram=numel(c_sgram);
75
76disp(' ');
77disp('The spectrogram is highly redundant.');
78fprintf('No. of coefficients in the signal:       %i\n',Ls);
79fprintf('No. of coefficients in the spectrogram:  %i\n',no_sgram);
80fprintf('Redundacy of the spectrogram:            %f\n',no_sgram/Ls);
81
82% WARNING: In the above code, the spectrogram routine SGRAM returns the
83% coefficients use to plot the image. These coefficients are ONLY
84% intended to be used by post-processing image tools, and in this
85% example, the are only used to illustrate the redundancy of the
86% spectogram. Numerical Gabor signal analysis and synthesis should ALWAYS
87% be done using the DGT, IDGT, DGTREAL and IDGTREAL functions, see the
88% following sections of this example.
89
90disp(' ');
91disp('---- Simple Gabor analysis using a standard Gaussian window. ----');
92
93disp('Setup parameters for a Discrete Gabor Transform.')
94disp('Time shift:')
95a=20
96
97disp('Number of frequency channels.');
98M=40
99
100disp(' ');
101disp('Note that it must hold that L = M*b = N*a for some integers b, N and L,');
102disp('and that a<M. L is the transform length, and the DGT will choose the');
103disp('smallest possible value of L that is larger or equal to the length of the');
104disp('signal. Choosing a<M makes the transform redundant, otherwise the');
105disp('transform will be lossy, and reconstruction will not be possible.');
106
107% Simple DGT using a standard Gaussian window.
108c=dgt(f,'gauss',a,M);
109
110disp('Number of time shifts in transform:')
111N = size(c,2);
112
113disp('Length of transform:')
114L = N*a
115
116
117figure(2);
118plotdgt(c,a,'linsq');
119title('Gabor coefficients.');
120
121disp(' ');
122disp(['The redundancy of the Gabor transform can be reduced without loosing ' ...
123      'information.']);
124fprintf('No. of coefficients in the signal:       %i\n',Ls);
125fprintf('No. of output coefficients from the DGT: %i\n',numel(c));
126fprintf('Redundacy of the DGT (in this case)      %f\n',numel(c)/Ls);
127
128disp(' ');
129disp('---- Real valued Gabor analysis. ----');
130
131% Figure 1 and Figure 2 looks quite different, because Figure 2 also
132% displays the coefficients for the n
133
134% Simple real valued DGT using a standard Gaussian window.
135c_real=dgtreal(f,'gauss',a,M);
136
137figure(3);
138plotdgtreal(c_real,a,M,'linsq');
139title('Positive-frequency DGT coefficients (DGTREAL).');
140
141figure(4);
142b=L/M;
143[X,Y]=meshgrid(1:a:L+a,1:b:L/2+b);
144
145hold on;
146imagesc(c_sgram);
147plot([X(:),X(:)]',[Y(:),Y(:)]','wo','Linewidth',1);
148axis('xy','image');
149hold off;
150title('Placement of the DGTREAL coefficients on the spectrogram.');
151
152disp(' ');
153disp('---- Perfect reconstruction. ----');
154
155% Reconstruction from the full DGT coefficients
156r      = idgt(c,'gaussdual',a);
157
158% Reconstruction from the DGTREAL coefficients
159% The parameter M cannot be deduced from the size of the coefficient
160% array c_real, so it is an explicit input parameter.
161r_real = idgtreal(c_real,'gaussdual',a,M);
162
163fprintf('Reconstruction error using IDGT:      %e\n',norm(f-r));
164fprintf('Reconstruction error using IDGTREAL:  %e\n',norm(f-r_real));
165
166