[d20d5a]: fourier / magresp.m Maximize Restore History

Download this file

magresp.m    156 lines (128 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function magresp(g,varargin);
%MAGRESP Magnitude response plot of window
% Usage: magresp(g,...);
% magresp(g,fs,...);
% magresp(g,fs,dynrange,....);
%
% `magresp(g)` will display the magnitude response of the window on a log
% scale (dB);
%
% `magresp(g,fs)` does the same for windows that are intended to be used
% with signals with sampling rate *fs*. The x-axis will display Hz.
%
% `magresp(g,fs,dynrange)` will limit the dynamic range (see below).
%
% `magresp` takes the following parameters at the end of the line of
% input arguments.
%
% 'dynrange',r Limit the dynamic range of the plot to r dB.
%
% 'fir' Indicate that the input is an FIR window. |magresp|_ will
% zero-extend the window to display a smooth magnitude
% response.
%
% 'L',L Zero-extend the window to length L.
%
% 'posfreq' Show only positive frequencies.
%
% 'nf' Show also negative frequencies
%
% 'autoposfreq' Show positive frequencies for real-valued signals,
% otherwise show also the negative frequencies. This is
% the default.
%
% In addition to these flags, it is possible to speficy any of the
% normalization flags from |normalize|_ to normalize the input before
% calculation of the magnitude response. Specifying `'1'` or `'area'` will
% display a magnitude response which peaks at 0 dB.
%
% Examples:
% ---------
%
% The following will display the magnitude response of a Hann window
% of length 20:::
%
% magresp({'hann',20});
%
% The following will display the magnitude response of a Gaussian window
% of length 100:::
%
% magresp('gauss','L',100);
%
% Demos: demo_gabfir
% AUTHOR : Peter L. S��ndergaard.
% TESTING: NA
% REFERENCE: NA
if nargin<1
error('Too few input arguments.');
end;
L=[];
fs=[];
donf=0;
% Define initial value for flags and key/value pairs.
definput.flags.posfreq={'autoposfreq','posfreq','nf'};
definput.import={'normalize'};
definput.importdefaults={'null'};
definput.keyvals.fs=[];
definput.keyvals.opts={};
definput.keyvals.L=[];
definput.flags.wintype={'notype','fir','long'};
definput.keyvals.dynrange=[];
[flags,kv,fs]=ltfatarghelper({'fs','dynrange'},definput,varargin);
[g,info] = comp_fourierwindow(g,kv.L,'MAGRESP');
do_real=flags.do_posfreq;
if flags.do_autoposfreq
do_real=isreal(g);
end;
if flags.do_fir
info.isfir=1;
end;
if isempty(kv.L)
if info.isfir
% Choose a strange length, such that we don't accidentically hit all
% the zeros in the response.
kv.L=length(g)*13+47;
else
kv.L=length(g);
end;
end;
g=fir2long(g,kv.L);
g=normalize(g,flags.norm);
if do_real
% Compute spectrum and normalize
FF=abs(fftreal(real(g)));
% Convert to dB. Add eps to avoid log of zero.
FF=20*log10(FF+realmin);
xmin=0;
else
% Compute spectrum and normalize. fftshift to center correctly for plotting.
FF=fftshift(abs(fft(g)));
% Convert to dB. Add eps to avoid log of zero.
FF=20*log10(FF+realmin);
xmin=-1;
end;
ymax=max(FF);
if ~isempty(kv.dynrange)
ymin=ymax-kv.dynrange;
else
ymin=min(FF);
end;
Lplot=length(FF);
% Only plot positive frequencies for real-valued signals.
if isempty(fs)
xrange=linspace(xmin,1,Lplot).';
axisvec=[xmin 1 ymin ymax];
else
xrange=linspace(xmin*floor(fs/2),floor(fs/2),Lplot).';
axisvec=[xmin*fs/2 fs/2 ymin ymax];
end;
plot(xrange,FF,kv.opts{:});
set(gca,'yscale','linear');
axis(axisvec);
ylabel('Magnitude response (dB)');
if isempty(fs)
xlabel('Frequency (normalized) ');
else
xlabel('Frequency (Hz)');
end;
legend('off');