function [wave,period] = waveletp(x,dt,period); % waveletp -- Modification of "wavelet". C.R. Denham, ZYDECO. % waveletp('demo') gives a demonstration. % % WAVELETP Wavelet transform with specified period. % [wave,period] = waveletp(x, dt, period) % Computes the wavelet transform of the vector x (length n), % with sampling rate dt and specified period in units compatible % with the given dt. % wave is the complex wavelet transform, na-by-n complex matrix, % where na=4*log(n*dt)/log(2dt) is the number of scales. % period is a vector (length na) of the Fourier periods corresponding % to the chosen scales. % % By default, the Morlet wavelet (k0=6) is used. % The wavelet basis is normalized to have total energy=1 at all scales. % % URL: "http://marigold.colorado.edu/research/wavelets/" % Copyright (C) 1995-1997, Chris Torrence, % University of Colorado, Program in Atmospheric and Oceanic Sciences. % This software may be used, copied, or redistributed as long as it is not % sold and this copyright notice is reproduced on each copy made. This % routine is provided as is without any express or implied warranties % whatsoever. if nargin < 1 help(mfilename) x = 'demo'; end % ZYDECO addition. if nargin < 2, dt = 1; end % ZYDECO addition. if strcmp(x, 'demo') % ZYDECO addition. n = 120; dt = 1; t = (0:n-1)*dt; x = (cos(3*2*pi*t/n) + 2.* (rand(size(t))-0.5)) ./ 2; p = 25:2:55; [w, p] = waveletp(x, dt, p); figure(gcf) set(gcf, 'Name', 'WaveletP Demo') subplot(3, 1, 1) plot(t, x) ylabel('data') title('Noisy Cosine') subplot(3, 1, 2) contour(t, p, abs(w), 20) ylabel('period') title('Wavelet Transform Magnitude') subplot(3, 1, 3) contour(t, p, angle(w), 20) xlabel('time') ylabel('period') title('Wavelet Transform Phase') return end f = fft(x); n = length(x); k = [1:fix(n/2)]; k = k.*((2.*pi)/(n*dt)); k = [0., k, -k(fix((n-1)/2):-1:1)]; a0 = 2.*dt; oct = fix(log(n*dt/a0)/log(2)+0.999); voice = 4; na = oct*voice + 1; k0 = 6.; % From below. if nargin < 3 ascale = a0*2.^((0:na-1)/voice); period = ascale; else % ZYDECO addition. scale = (4*pi)/(k0 + sqrt(2 + k0^2)); % From below. period = period ./ scale; ascale = period; end na = length(period); % ZYDECO addition. if (0) wave = []; else wave = zeros(length(period), length(x)); % ZYDECO addition. end % Loop through all scales and compute transform. for a1 = 1:na % Morlet wavelet. a = ascale(a1); k0 = 6.; expnt = -(a.*k - k0).^2/2.*(k > 0.); factor = sqrt(a*k(2)*2.*sqrt(pi)); morlet = factor/sqrt(2*pi)*exp(expnt); morlet = morlet.*(k > 0.); period(a1) = period(a1).*(4*pi)/(k0 + sqrt(2 + k0^2)); % Wavelet transform. if (0) wave = [wave ; ifft(f.*morlet)]; else wave(a1, :) = ifft(f.*morlet); % ZYDECO addition. end end