David Luong ‘06 Engineering 78 Lab 1 Part 2 CONV Convolution and polynomial multiplication. C = CONV(A, B) convolves vectors A and B. The resulting vector is length LENGTH(A)+LENGTH(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials. Class support for inputs A,B: float: double, single See also deconv, conv2, convn, filter and, in the Signal Processing Toolbox, xcorr, convmtx. Reference page in Help browser doc conv >> a=[1 2 1] a= 1 2 1 >> b = [2 4 2] b= 2 4 2 >> conv(a,b) ans = 2 8 12 8 2 CONV2 Two dimensional convolution. C = CONV2(A, B) performs the 2-D convolution of matrices A and B. If [ma,na] = size(A) and [mb,nb] = size(B), then size(C) = [ma+mb-1,na+nb-1]. C = CONV2(H1, H2, A) convolves A first with the vector H1 along the rows and then with the vector H2 along the columns. C = CONV2( ... ,'shape') returns a subsection of the 2-D convolution with size specified by 'shape': 'full' - (default) returns the full 2-D convolution, 'same' - returns the central part of the convolution that is the same size as A. 'valid' - returns only those parts of the convolution that are computed without the zero-padded edges. size(C) = [ma-mb+1,na-nb+1] when all(size(A) >= size(B)), otherwise C is empty. See also conv, convn, filter2 and, in the Signal Processing Toolbox, xcorr2. Overloaded functions or methods (ones with the same name in other directories) help uint8/conv2.m help uint16/conv2.m Reference page in Help browser doc conv2 >> a=[1 2 1; 2 1 2; 2 3 4]; b = [6 6 7; 4 5 3; 3 6 8]; >> c=conv2(a, b) c= 6 18 25 20 7 David Luong ‘06 Engineering 78 Lab 1 Part 2 16 31 49 30 17 23 56 98 80 42 14 37 65 49 28 6 21 46 48 32 CONVMTX Convolution matrix. CONVMTX(C,N) returns the convolution matrix for vector C. If C is a column vector and X is a column vector of length N, then CONVMTX(C,N)*X is the same as CONV(C,X). If R is a row vector and X is a row vector of length N, then X*CONVMTX(R,N) is the same as CONV(R,X). See also conv. Reference page in Help browser doc convmtx >> c=[1 2 1]; x=[3 2 1]; >> n=length(x); >> x*convmtx(c,n) ans = 3 8 8 4 1 >> conv(c,x) ans = 3 8 8 4 1 DECONV Deconvolution and polynomial division. [Q,R] = DECONV(B,A) deconvolves vector A out of vector B. The result is returned in vector Q and the remainder in vector R such that B = conv(A,Q) + R. If A and B are vectors of polynomial coefficients, deconvolution is equivalent to polynomial division. The result of dividing B by A is quotient Q and remainder R. Class support for inputs B,A: float: double, single See also conv, residue. Reference page in Help browser doc deconv >> a=[1 2 4]; b=[5 4 3]; >> [q,r]=deconv(b,a) q= 5 r= 0 -6 -17 >> b=conv(a,q) + r b= 5 4 3 David Luong ‘06 Engineering 78 Lab 1 Part 2 XCORR Cross-correlation function estimates. C = XCORR(A,B), where A and B are length M vectors (M>1), returns the length 2*M-1 cross-correlation sequence C. If A and B are of different length, the shortest one is zero-padded. C will be a row vector if A is a row vector, and a column vector if A is a column vector. XCORR produces an estimate of the correlation between two random (jointly stationary) sequences: C(m) = E[A(n+m)*conj(B(n))] = E[A(n)*conj(B(n-m))] It is also the deterministic correlation between two deterministic signals. XCORR(A), when A is a vector, is the auto-correlation sequence. XCORR(A), when A is an M-by-N matrix, is a large matrix with 2*M-1 rows whose N^2 columns contain the cross-correlation sequences for all combinations of the columns of A. The zeroth lag of the output correlation is in the middle of the sequence, at element or row M. XCORR(...,MAXLAG) computes the (auto/cross) correlation over the range of lags: -MAXLAG to MAXLAG, i.e., 2*MAXLAG+1 lags. If missing, default is MAXLAG = M-1. [C,LAGS] = XCORR(...) returns a vector of lag indices (LAGS). XCORR(...,SCALEOPT), normalizes the correlation according to SCALEOPT: 'biased' - scales the raw cross-correlation by 1/M. 'unbiased' - scales the raw correlation by 1/(M-abs(lags)). 'coeff' - normalizes the sequence so that the auto-correlations at zero lag are identically 1.0. 'none' - no scaling (this is the default). See also xcov, corrcoef, conv, cov and xcorr2. Reference page in Help browser doc xcorr 60 xcorr(a,a) xcorr(a,b) xcorr(b,b) 40 20 0 -20 -40 -60 0 50 100 150 200 250 David Luong ‘06 Engineering 78 Lab 1 Part 2 >> t=-5:.1:5; >> a=sin(t); b=cos(t); >> x=xcorr(a,a); y=xcorr(a,b); >> hold on >> h=1:length(x); >> z=xcorr(b,b); >> legend('xcorr(a,a)', 'xcorr(a,b)', 'xcorr(b,b)') >> plot(h,x,h,y,h,z) >> legend('xcorr(a,a)', 'xcorr(a,b)', 'xcorr(b,b)') XCORR2 Two-dimensional cross-correlation. XCORR2(A,B) computes the crosscorrelation of matrices A and B. XCORR2(A) is the autocorrelation function. See also conv2, xcorr and filter2. Reference page in Help browser doc xcorr2 >> a=[1 2 3; 1 2 3; 1 2 4]; b=[ 3 4 5; 5 6 7; 7 4 1]; >> xcorr2(a,a) ans = 4 7 10 6 3 10 18 26 16 8 17 31 49 31 17 8 16 26 18 10 3 6 10 7 4 26 54 76 52 22 21 36 52 29 12 >> xcorr2(a,b) ans = 1 8 13 12 5 6 26 40 34 14 18 56 83 71 31 >> xcorr2(b,b) ans = 3 26 57 74 35 16 72 136 120 48 42 48 35 140 120 74 226 136 57 140 72 26 42 16 3 FFT Discrete Fourier transform. FFT(X) is the discrete Fourier transform (DFT) of vector X. For matrices, the FFT operation is applied to each column. For N-D arrays, the FFT operation operates on the first non-singleton dimension. FFT(X,N) is the N-point FFT, padded with zeros if X has less than N points and truncated if it has more. FFT(X,[],DIM) or FFT(X,N,DIM) applies the FFT operation across the dimension DIM. For length N input vector x, the DFT is a length N vector X, with elements N X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N. David Luong ‘06 Engineering 78 Lab 1 Part 2 n=1 The inverse DFT (computed by IFFT) is given by N x(n) = (1/N) sum X(k)*exp( j*2*pi*(k-1)*(n-1)/N), 1 <= n <= N. k=1 See also fft2, fftn, fftshift, fftw, ifft, ifft2, ifftn. Overloaded functions or methods (ones with the same name in other directories) help uint8/fft.m help uint16/fft.m Reference page in Help browser doc fft 90 80 70 60 50 40 30 20 10 0 -10 0 20 40 60 80 100 120 140 >> x=sin(t); >> plot(1:length(x),fft(x)) FFT2 Two-dimensional discrete Fourier Transform. FFT2(X) returns the two-dimensional Fourier transform of matrix X. If X is a vector, the result will have the same orientation. FFT2(X,MROWS,NCOLS) pads matrix X with zeros to size MROWS-by-NCOLS before transforming. Class support for input X: float: double, single See also fft, fftn, fftshift, fftw, ifft, ifft2, ifftn. Reference page in Help browser doc fft2 >> fft2(a,3,3) ans = 19.0000 -5.0000 + 3.4641i -5.0000 - 3.4641i 160 180 200 David Luong ‘06 Engineering 78 Lab 1 Part 2 -0.5000 + 0.8660i -0.5000 - 0.8660i 1.0000 -0.5000 - 0.8660i 1.0000 -0.5000 + 0.8660i >> fft2(b,3,3) ans = 42.0000 1.5000 - 0.8660i 1.5000 + 0.8660i -3.0000 - 5.1962i 0 + 6.9282i -6.0000 + 3.4641i -3.0000 + 5.1962i -6.0000 - 3.4641i 0 - 6.9282i IFFT Inverse discrete Fourier transform. IFFT(X) is the inverse discrete Fourier transform of X. IFFT(X,N) is the N-point inverse transform. IFFT(X,[],DIM) or IFFT(X,N,DIM) is the inverse discrete Fourier transform of X across the dimension DIM. IFFT(..., 'symmetric') causes IFFT to treat F as conjugate symmetric along the active dimension. This option is useful when F is not exactly conjugate symmetric merely because of round-off error. See the reference page for the specific mathematical definition of this symmetry. IFFT(..., 'nonsymmetric') causes IFFT to make no assumptions about the symmetry of F. See also fft, fft2, fftn, fftshift, fftw, ifft2, ifftn. Overloaded functions or methods (ones with the same name in other directories) help uint8/ifft.m help uint16/ifft.m Reference page in Help browser doc ifft 45 40 35 30 25 20 15 10 5 0 -5 >> a=cos(t); >> fft(a); 0 20 40 60 80 100 120 140 160 180 200 David Luong ‘06 Engineering 78 Lab 1 Part 2 >> plot(t,ifft(fft(a))) >> a=cos(t); >> fft(a); >> hold on >> plot(1:length(fft(a)),fft(a)) Warning: Imaginary parts of complex X and/or Y arguments ignored. FFTSHIFT Shift zero-frequency component to center of spectrum. For vectors, FFTSHIFT(X) swaps the left and right halves of X. For matrices, FFTSHIFT(X) swaps the first and third quadrants and the second and fourth quadrants. For N-D arrays, FFTSHIFT(X) swaps "half-spaces" of X along each dimension. FFTSHIFT(X,DIM) applies the FFTSHIFT operation along the dimension DIM. FFTSHIFT is useful for visualizing the Fourier transform with the zero-frequency component in the middle of the spectrum. Class support for input X: float: double, single See also ifftshift, fft, fft2, fftn, circshift. Reference page in Help browser doc fftshift 45 40 35 30 25 20 15 10 5 0 -5 0 20 40 60 80 100 120 140 SPECTRUM Power spectrum estimate of one or two data sequences. SPECTRUM has been replaced by SPECTRUM.WELCH. SPECTRUM still works but may be removed in the future. Use SPECTRUM.WELCH (or its functional form PWELCH) instead. Type help SPECTRUM/WELCH for details. See also spectrum/psd, spectrum/msspectrum, spectrum/periodogram. Overloaded functions or methods (ones with the same name in other directories) help dspopts/spectrum.m Reference page in Help browser 160 180 200 David Luong ‘06 Engineering 78 Lab 1 Part 2 doc spectrum >> a=sin(t); b=cos(t); >> spectrum(a,b) Pxx - X Power Spectral Density 2 10 0 10 -2 10 -4 10 -6 10 -8 10 -10 10 -12 10 -14 10 0 0.1 0.2 0.3 0.4 0.5 0.6 Frequency 0.7 0.8 0.9 1 0.8 0.9 1 Pyy - Y Power Spectral Density 2 10 0 10 -2 10 -4 10 -6 10 -8 10 -10 10 -12 10 -14 10 0 0.1 0.2 0.3 0.4 0.5 0.6 Frequency 0.7 David Luong ‘06 Engineering 78 Lab 1 Part 2 Txy - Transfer function magnitude 0.2 10 0.1 10 0 10 -0.1 10 -0.2 10 0 0.1 0.2 0.3 0.4 0.5 0.6 Frequency 0.7 0.8 0.9 1 0.8 0.9 1 Txy - Transfer function phase 200 150 100 50 0 -50 -100 -150 -200 0 0.1 0.2 0.3 0.4 0.5 0.6 Frequency 0.7 David Luong ‘06 Engineering 78 Lab 1 Part 2 Cxy - Coherence 1 1 1 1 1 1 1 1 0 0.1 0.2 0.3 0.4 0.5 0.6 Frequency specplot PLOT Plot method for dspdata objects. PLOT(H) plots the data in the object H. EXAMPLE: Use the periodogram to estimate the power spectral density of a noisy sinusoidal signal with two frequency components. Then store the results in PSD data object and plot it. Fs = 32e3; t = 0:1/Fs:2.96; x = cos(2*pi*t*1.24e3)+ cos(2*pi*t*10e3)+ randn(size(t)); Pxx = periodogram(x); hpsd = dspdata.psd(Pxx,'Fs',Fs); % Create a PSD data object. plot(hpsd); % Plot the PSD. 0.7 0.8 0.9 1 David Luong ‘06 Engineering 78 Lab 1 Part 2 Power Spectral Density 40 30 20 Power/frequency (dB/Hz) 10 0 -10 -20 -30 -40 -50 -60 0 2 4 6 8 10 Frequency (kHz) 12 14 16