Math 56 Homework 5 i6x Michael Downs −i6x 1. (a) Since f (x) = cos(6x) = e 2 + e 2 , due to the orthogonality of each einx , n ∈ Z, the only nonzero (complex) fourier coefficients are fˆ6 and fˆ−6 and they’re both 12 (which is also seen from Euler’s identity). (b) The function f (x) = e6ix lies outside the nyquist frequency N2 = 5 so aliasing will occur. The DFT coefficients will approximate f˜m for integers −N ≤ m < N2 each with 2 error · · ·+ fˆm−2N + fˆm−N + fˆm+N fˆm+2N +· · · (these are the exact fourier coefficients). Since the only nonzero fourier coefficient is fˆ6 = 1, the only nonzero approximation in the DFT will be f˜−4 since its error hits fˆ6 , the only nonzero fourier coefficient. The interpolated function will be f˜(x) = e−4ix . 2. (a) Code: 1 % spectral differentiation 3 5 N = 4 0 ; % number o f sample p o i n t s f = @( x ) exp ( s i n ( x ) ) ; % f u n c t i o n we ’ r e s a m p l i n g dF = @( x ) exp ( s i n ( x ) ) . ∗ c o s ( x ) ; % i t s d e r i v a t i v e 7 g r = 0 : 1 e −3:2∗ p i ; % t h e f i n e s t g r i d 9 m a x e r r o r s = z e r o s ( 1 , N/ 2 ) ; n = 2 : 2 :N; 11 13 15 f o r j = 2 : 2 :N x = l i n s p a c e ( 0 , ( j −1)/ j ∗ 2 ∗ pi , j ) ; % sample x v a l u e s y = f ( x ) . / j ; % samples f t = f f t ( y ) ; % approximated c o e f f i c i e n t s % g e t d e r i v a t i v e c o e f f i c i e n t s by m u l t i p l y i n g each approximated % c o e f f i c i e n t by i ∗m where m i s i t s f r e q u e n c y / c o e f f i c i e n t number f o r k = 1 : j /2 f t ( k ) = f t ( k ) ∗ i ∗ ( k−1) ; f t ( j + 1 − k ) = f t ( j + 1 − k ) ∗ i ∗ −k ; end 17 19 21 23 i n t e r = @( x ) r e a l ( sum ( f t ( ( j /2 + 1 ) : j ) . ∗ exp (−1∗ i ∗x ∗ ( j / 2 : − 1 : 1 ) ) ) + sum ( f t ( 1 : j / 2 ) . ∗ exp ( i ∗x ∗ ( 0 : ( j /2 − 1 ) ) ) ) ) ; e r r o r = max( abs ( dF ( g r ) − a r r a y f u n ( i n t e r , g r ) ) ) ; maxerrors ( j /2) = e r r o r ; 25 27 end 29 figure ; semilogy (n , maxerrors ) ; x l a b e l ( ’N ’ ) ; y l a b e l ( ’max e r r o r ’ ) ; t i t l e ( ’ E r r o r i n t r u n c a t e d N term a p p r o x i m a t i o n u s i n g s p e c t r a l differentiation ’) 31 33 1 Math 56 Homework 5 Michael Downs Plot: Error in truncated N term approximation using spectral differentiation 2 10 0 10 −2 10 −4 max error 10 −6 10 −8 10 −10 10 −12 10 −14 10 −16 10 0 5 10 15 20 N 25 30 35 (b) Replacing f and df with: 1 f = @( x ) abs ( s i n ( x ) ) . ˆ 3 ; % f u n c t i o n we ’ r e s a m p l i n g dF = @( x ) 3∗ s i n ( x ) . ∗ abs ( s i n ( x ) ) . ∗ c o s ( x ) ; % i t s d e r i v a t i v e and plotting log(y) vs x: 2 40 Math 56 Homework 5 Michael Downs Error in truncated N term approximation using spectral differentiation 1 10 0 10 −1 max error 10 −2 10 −3 10 −4 10 0 10 20 30 40 50 N 60 70 80 90 100 This is clearly not exponential convergence. I plot the errors again using log(y) vs log(x): Error in truncated N term approximation using spectral differentiation 1 10 0 10 −1 max error 10 −2 10 −3 10 −4 10 0 10 1 2 10 10 3 10 N It appears to be algebraic of order ≈ 2 judging from the graph. The function is twice differentiable so by the theorem from HW4 1(e) it should have second order algebraic. It is not infinitely differentiable and therefore does not have super algebraic convergence. 3 Math 56 Homework 5 Michael Downs 3. Algorithm: 2 % c o o l e y −tukey r e c u r s i v e a l g o r i t h m f o r t h e f a s t f o u r i e r t r a n s f o r m % t a k e s a row v e c t o r o f w e i g h t e d s a m p l e s o f l e n g t h 2ˆn % and o u t p u t s t h e t r a n s f o r m e d v e c t o r 4 6 8 10 function res = c t f f t ( f ) N = length ( f ) ; % b a s e c a s e i n r e c u r s i o n when s i z e i s 2 i f N == 2 r e s = ( dftmtx ( 2 ) ∗ f . ’ ) . ’ ; return ; end 12 % s h u f f l e i n t o even and odd v e c t o r s . KEEPING IN MIND THE ZERO INDEXING % SO ACTUALLY IT ’ S REVERSED f e = f ( 1 : 2 : N) ; f o = f ( 2 : 2 : N) ; 14 16 % r e c u r s e to get t h e i r f f t s e = ctf ft ( fe ) ; o = c t f f t ( fo ) ; 18 20 % m u l t i p l y o by t w i d d l e f a c t o r f o r combining l a t e r twid = exp(−1 ∗ 1 i ∗ 2 ∗ p i ∗ 1/N ∗ ( 0 : (N/2 − 1 ) ) ) ; 22 24 o = twid . ∗ o ; 26 % combine them r e s = z e r o s ( 1 , N) ; r e s ( 1 :N/ 2 ) = e ( 1 :N/ 2 ) + o ( 1 :N/ 2 ) ; r e s ( ( 1 :N/ 2 ) + N/ 2 ) = e ( 1 :N/ 2 ) − o ( 1 :N/ 2 ) ; 28 30 end driver: 1 % d r i v e r f o r my c t f f t code . computes norm o f e r r o r between matlab f f t and % my f f t . a l s o compares run t i m e s . 3 5 7 N = 2 ˆ 1 6 ; % number o f sample p o i n t s f = @( x ) exp ( s i n ( x ) ) ; % f u n c t i o n we ’ r e s a m p l i n g x = l i n s p a c e ( 0 , (N−1)/N ∗ 2 ∗ pi , N) ; % sample x v a l u e s y = f ( x ) . /N; % s a m p l e s 9 11 tic ; mlc = f f t ( y ) ; % matlab code l i b = toc ; 13 4 Math 56 15 Homework 5 Michael Downs tic ; myc = c t f f t ( y ) ; % my code mine = t o c ; 17 19 f p r i n t f ( ’ norm o f e r r o r : \n ’ ) norm ( mlc − myc ) 21 f p r i n t f ( ’ t h e l i b r a r y code i s %g t i m e s f a s t e r \n ’ , mine / l i b ) ; output: >> driver norm of error: ans = 6.9905e-16 the library code is 4290.64 times faster 4. (a) Code: 1 [ f , f s , n b i t s ] = wavread ( ’ s i g n o i s e . wav ’ ) ; 3 x = l i n s p a c e ( 0 , 2∗ p i ∗ ( l e n g t h ( f ) −1)/ l e n g t h ( f ) , l e n g t h ( f ) ) ; 5 7 plot (x , f ’ ) ; x l a b e l ( ’ time ’ ) y l a b e l ( ’ amplitude ’ ) t i t l e ( ’ s i g n a l f which c o n t a i n s one f r e q u e n c y component on top o f n o i s e ’ ) 5 Math 56 Homework 5 Michael Downs signal f which contains one frequency component on top of noise 1 0.8 0.6 0.4 amplitude 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 0 1 2 3 4 5 6 7 time There appears to be a subtle sinusoidal shape and some periodicity but it’s difficult to make out. Turning to the fft to determine the dominant frequency: 2 4 6 % u s e t h e f f t t o r e c o n s t r u c t t h e o r i g i n a l s i g n a l by d e t e r m i n i n g which % f o u r i e r c o e f f i c i e n t s a r e dominant ft = fft ( f ) ; p l o t ( 1 : l e n g t h ( f t ) , abs ( f t ) ) ; t i t l e ( ’ magnitude o f approximated f o u r i e r c o e f f i c i e n t s ’ ) ; x l a b e l ( ’m’ ) ; y l a b e l ( ’ abs ( f m ) ’ ) ; A graph of the magntudes of the fourier coeffiients shows one coefficient pair to be much higher than the others: 6 Math 56 Homework 5 Michael Downs magnitude of approximated fourier coefficients 450 400 350 abs(fm) 300 250 200 150 100 50 0 0 1 2 3 4 5 6 7 m 4 x 10 magnitude of approximated fourier coefficients 400 350 300 abs(fm) 250 200 150 100 50 0 651 652 653 654 655 m 656 657 658 zooming reveals that the dominant coefficient is at 655 in matlab so it’s at m = 654 which means that the other coefficient in the pair should be at 65536−654+1 = 64883 (and it is). The indices are related because the negative coefficients get wrapped into the upper half of the DFT output vector. The 654th fourier coefficient is in the first half but the -654th is at position 64883 in the output vector. The coefficients are related in that they are eachother’s complex conjugate. Confirming: >> ft(655) 7 Math 56 Homework 5 Michael Downs ans = 3.1442e+02 - 2.8471e+02i >> ft(64883) ans = 3.1442e+02 + 2.8471e+02i They both correspond to the dominant frequency for the input signal F which is, presumably, some combination of sines and cosines . (b) The true frequency for m = 654, fs = 44100, and N = 65536 should be: f= mfs ≈ 440Hz N BONUS I reconstruct the original signal: 1 3 % remove n o i s e f i x e d = z e r o s ( length ( f t ) , 1) ; fixed (655) = f t (655) ; fixed (64883) = f t (64883) ; 5 7 % reverse f f t sig = i f f t ( fixed ) ; wavwrite ( s i g , 4 4 1 0 0 , ’ f i x e d . wav ’ ) ; I cannot hear the tone above the noise but since the fourier coefficients corresponding to the undistorted signal are larger in amplitude than the noise, they should be able to be heard over the noise. 5. (a) Given f = [1, 1, 1, 0, 0, 0]T and g = [1, 1, 1, 1, 0, 0]T , f ∗ g = [1, 2, 3, 3, 2, 1]T . (b) Given f = [1, 1, 1, 0, 0]T and g = [1, 1, 1, 1, 0]T , f ∗ g = [2, 2, 3, 3, 2]T (c) (a) has the same elements as in the non periodic convolution. Both vectors must be padded to length N1 + N2 − 1. (d) ft and ift denote the fourier transform and inverse fourier transform. f ∗ g = ift(ft(f ∗ g))) = ift(ft(f ) ft(g)) = ift(ft(g) ft(f )) = ift(ft(g ∗ f )) =g∗f 8 Math 56 Homework 5 Michael Downs (e) The sum of the elements in f ∗ g in (a) is 12 which is 3 ∗ 4 while 3 and 4 are PN −1 the sums of the elements in f and g respectively. Proposition: j=0 (f ∗ g)j = P PN −1 N −1 i=0 fi ∗ j=0 N −1 X (f ∗ g)j = j=0 N −1 N −1 X X j=0 i=0 = = N −1 X j=0 6. 2 4 6 g(j−i)modN = PN −1 j=0 fi N −1 X i=0 j=0 N −1 X N −1 X i=0 PN −1 fi g(j−i)modN fi g(j−i)modN gj j=0 gj for all i. It’s the same summation in a different order. % s c r i p t t o u s e t h e f f t and i f f t t o c o n v o l v e an a u d i o f i l e with an i m p u l s e % response [ f1 , [ f2 , f1 = f2 = f s 1 , n b i t s 1 ] = wavread ( ’ i n v o k e r . wav ’ ) ; f s 2 , n b i t s 2 ] = wavread ( ’ i m p u l s e r e s p o n s e . wav ’ ) ; f1 ’ ; f2 ’ ; 8 10 12 14 16 18 20 % pad with z e r o s s o t h a t t h e c o n v o l u t i o n i s c o r r e c t padsize = length ( f1 ) + length ( f2 ) − 1; padf1 = z e r o s ( 1 , p a d s i z e − l e n g t h ( f 1 ) ) ; padf2 = z e r o s ( 1 , p a d s i z e − l e n g t h ( f 2 ) ) ; f 1 = [ f 1 padf1 ] ; f 2 = [ f 2 padf2 ] ; % convert to f o u r i e r space . f1 = f f t ( f1 ) ; f2 = f f t ( f2 ) ; % perform component w i s e m u l t i p l i c a t i o n res = f1 .∗ f2 ; 22 24 % s h i f t back t o r e g u l a r s p a c e res = i f f t ( res ) ; 26 res = res ’ ; 28 r e s = r e s /max( r e s ) ; 9 Math 56 30 Homework 5 Michael Downs wavwrite ( r e s , 4 4 1 0 0 , ’ r e s u l t . wav ’ ) ; 7. (a) 2 4 6 blur = reshape ( textread ( ’ blurry . txt ’ ) ,512 ,512) ; aper = reshape ( textread ( ’ aperture . txt ’ ) ,512 ,512) ; axis equal ; colormap ( gray ( 2 5 6 ) ) imagesc ( blur ) ; 50 100 150 200 250 300 350 400 450 500 50 100 150 200 (b) 2 % convert to f o u r i e r space fblur = f f t 2 ( blur ) ; f a p e r = f f t 2 ( aper ) ; 4 6 % d e c o n v o l v e and c o n v e r t back clear = fblur ./ faper ; clear = ifft2 ( clear ) ; 8 imagesc ( c l e a r ) ; 10 250 300 350 400 450 500 Math 56 Homework 5 Michael Downs 50 100 150 200 250 300 350 400 450 500 50 100 150 200 250 300 350 400 450 500 200 250 300 350 400 450 500 (c) 1 3 5 7 9 11 % add a s m a l l amount o f e r r o r n = length ( blur ) ; e r b l u r = b l u r + randn ( n ) ∗3∗10ˆ −5 % convert to f o u r i e r space fblur = fft2 ( erblur ) ; f a p e r = f f t 2 ( aper ) ; % d e c o n v o l v e and c o n v e r t back clear2 = fblur ./ faper ; clear2 = i f f t 2 ( clear2 ) ; imagesc ( c l e a r 2 ) ; 50 100 150 200 250 300 350 400 450 500 50 100 150 Why does the error get amplified? In the component wise division in fourier space, most of the entries in the aperture matrix are small, some on the order of 10−6 . Divison by such small numbers amplifies the 10−5 error. A visualization of the dft of the aperture function is below. The blue signifies relatively smaller values while the 11 Math 56 Homework 5 Michael Downs red/green signifies relatively larger values. The majority of values in the dft of the aperture matrix are small and this particular deconvolution process amplifies errors 10−5 or larger. 50 100 150 200 250 300 350 400 450 500 50 100 150 200 12 250 300 350 400 450 500
© Copyright 2026 Paperzz