www.gusucode.com > wavelet 源码程序 matlab案例代码 > wavelet/LocalizeDiscontinuityInSineWaveExample.m
%% Localize Discontinuity in Sine Wave % This example shows wavelet analysis can localize a discontinuity in a % sine wave. %% % Create a 1-Hz sine wave sampled at 100 Hz. The duration of the sine wave % is one second. The sine wave has a discontinuity at $t=0.5$ seconds. % Copyright 2015 The MathWorks, Inc. t = linspace(0,1,100)'; x = sin(2*pi*t); x1 = x-0.15; y = zeros(size(x)); y(1:length(y)/2) = x(1:length(y)/2); y(length(y)/2+1:end) = x1(length(y)/2+1:end); stem(t,y,'markerfacecolor',[0 0 1]); xlabel('Seconds'); ylabel('Amplitude'); %% % Obtain the nondecimated discrete wavelet transform of the sine wave using % the |'sym2'| wavelet and plot the wavelet (detail) % coefficients along with the original signal. [swa,swd] = swt(y,1,'sym2'); subplot(211) stem(t,y,'markerfacecolor',[0 0 1]); title('Orignal Signal'); subplot(212) stem(t,swd,'markerfacecolor',[0 0 1]); title('Level 1 Wavelet Coefficients'); %% % Compare the Fourier coefficient magnitudes for the 1-Hz sine wave with % and without the discontinuity. dftsig = fft([x y]); dftsig = dftsig(1:length(y)/2+1,:); df = 100/length(y); freq = 0:df:50; stem(freq,abs(dftsig)); xlabel('Hz'); ylabel('Magnitude'); legend('sine wave','sine wave with discontinuity'); %% % There is minimal difference in the magnitudes of the Fourier % coefficients. Because the discrete Fourier basis vectors have support % over the entire time interval, the discrete Fourier transform does not % detect the discontinuity as efficiently as the wavelet transform. %% % Compare the level 1 wavelet coefficients for the sine wave with and % without the discontinuity. [swax,swdx] = swt(x,1,'sym2'); subplot(211) stem(t,swd); title('Sine Wave with Discontinuity (Wavelet Coefficients)'); subplot(212) stem(t,swdx); title('Sine Wave (Wavelet Coefficients)'); %% % The wavelet coefficients of the two signals demonstrate a significant % difference. % Wavelet analysis is often capable of revealing characteristics of a % signal or image that other analysis techniques miss, like trends, % breakdown points, discontinuities in higher derivatives, and % self-similarity. Furthermore, because wavelets provide a different view % of data than those presented by Fourier techniques, wavelet analysis can % often significantly compress or denoise a signal without appreciable % degradation.