[ea1ce1]: fourier / plotfft.m Maximize Restore History

Download this file

plotfft.m    145 lines (122 with data), 4.0 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
function plotfft(coef,varargin)
%PLOTFFT Plot the output from FFT
% Usage: plotfft(coef);
% plotfft(coef,fs);
%
% `plotfft(coef)` plots the output from the `fft` function. The
% frequency axis will use normalized frequencies between 0 and 1 (the
% Nyquist frequency).
%
% `plotfft(coef,fs)` does the same for the FFT of a signal sampled at
% a sampling rate of *fs* Hz.
%
% `plotfft(coef,fs,dynrange)` additionally limits the dynamic range of the
% plot. See the description of the `'dynrange'` parameter below.
%
% `plotfft` accepts the following optional arguments:
%
% 'dynrange',r Limit the dynamical range to `r` by using a colormap in
% the interval `[chigh-r,chigh]`, where `chigh` is the highest
% value in the plot. The default value of `[]` means to not
% limit the dynamical range.
%
% 'db' Apply $20\cdot \log_{10}$ to the coefficients. This makes
% it possible to see very weak phenomena, but it might show
% too much noise. This is the default.
%
% 'dbsq' Apply $10\cdot \log_{10}$ to the coefficients. Same as the
% `'db'` option, but assumes that the input is already squared.
%
% 'lin' Show the coefficients on a linear scale. This will
% display the raw input without any modifications. Only works for
% real-valued input.
%
% 'linsq' Show the square of the coefficients on a linear scale.
%
% 'linabs' Show the absolute value of the coefficients on a linear
% scale.
%
% 'nf' Display negative frequencies, with the zero-frequency
% centered in the middle. This is the default.
%
% 'posfreq' Display only the positive frequencies.
%
%
% In addition to these parameters, `plotfft` accepts any of the flags
% from |normalize|. The coefficients will be normalized as specified
% before plotting.
%
% See also: plotfftreal
if nargin<1
error('%s: Too few input parameters.',upper(mfilename));
end;
if ~isvector(coef)>1
error('Input is multidimensional.');
end;
definput.import={'ltfattranslate','normalize'};
definput.importdefaults={'null'};
definput.flags.log={'db','dbsq','lin','linsq','linabs'};
definput.flags.posfreq={'nf','posfreq'};
definput.keyvals.fs=[];
definput.keyvals.dynrange=[];
definput.keyvals.opts={};
[flags,kv,fs]=ltfatarghelper({'fs','dynrange'},definput,varargin);
coef=normalize(coef,flags.norm);
if ~isvector(coef)
error('%s: Can only plot vectors.',upper(mfilename));
end;
N=length(coef);
% Apply transformation to coefficients.
if flags.do_db
coef=20*log10(abs(coef)+realmin);
end;
if flags.do_dbsq
coef=10*log10(abs(coef)+realmin);
end;
if flags.do_linsq
coef=abs(coef).^2;
end;
if flags.do_linabs
coef=abs(coef);
end;
if flags.do_lin
if ~isreal(coef)
error(['Complex valued input cannot be plotted using the "lin" flag.',...
'Please use the "linsq" or "linabs" flag.']);
end;
end;
% 'dynrange' parameter is handled by thresholding the coefficients.
if ~isempty(kv.dynrange)
maxclim=max(coef(:));
coef(coef<maxclim-kv.dynrange)=maxclim-kv.dynrange;
end;
if flags.do_nf
if rem(N,2)==0
xr=(-N/2+1:N/2)*2/N;
% Subtract 1 in order to place the Nyquist frequency following the
% positive frequencies. That is why we do not use fftshift.
coef=circshift(coef,N/2-1);
else
xr=(-floor(N/2):floor(N/2))*2/N;
coef=fftshift(coef);
end;
else
N2=floor(N/2)+1;
coef=coef(1:N2);
xr=(0:N2-1)*2/N;
end;
if ~isempty(kv.fs)
xr=xr*kv.fs/2;
end;
plot(xr,coef,kv.opts{:});
xlim([xr(1) xr(end)]);
if flags.do_db || flags.do_dbsq
ylabel(sprintf('%s (dB)',kv.magnitude));
else
ylabel(sprintf('%s',kv.magnitude));
end;
if ~isempty(kv.fs)
xlabel(sprintf('%s (Hz)',kv.frequency));
else
xlabel(sprintf('%s (%s)',kv.frequency,kv.normalized));
end;