N06 Digital Signals Analog signals - continuous a) amplitude b) time Digital signals - discrete values a) amplitude b) time Amplitude digitizer must have sufficient 1) span 2) resolution 3) accuracy Time digitzer must have sufficient 1) speed - convert and store 2) capacity 3) precision Fixed sampling rate h = time interval between samples (constant) fS = 1 / h page 1 of 22 N06 Time domain Digital domain page 2 of 22 xt for 0 t t MAX x i for 1 i n at t i h i 1 x(t) t xi N06 page 3 of 22 Aliasing 30/60/90/120 Hz sampled at 200 Hz 1 0 -1 150/180/210/240 Hz sampled at 200 Hz 1 0 0 0.05 0.1 -1 1 1 0 0 -1 0 0.05 0.1 -1 1 1 0 0 -1 0 0.05 0.1 -1 1 1 0 0 -1 0 0.05 Time (sec) 0.1 -1 0 0.05 0.1 0 0.05 0.1 0 0.05 0.1 0 0.05 Time (sec) 0.1 Aliasing = fs/2 - abs( mod(f,fs) - fs/2 ) 100 Apparent frequency [Hz] for fs=200 Hz 90 80 70 60 50 40 30 20 10 0 0 50 100 150 200 250 Actual frequency [Hz] 300 350 400 N06 % alias.m - aliasing due to undersampling % HJSIII - 07.11.12 clear % actual sampling rate 200 Hz fs = 200; % sampling rate for display fsfast=2000; % sampling times dt = 1 / fs; dtfast = 1 / fsfast; % fill sample buffers tend = 0.1; ns = tend / dt; nsfast = tend / dtfast; ts = (0:ns) * dt; v30 = sin( 2 * pi * 30 * v60 = sin( 2 * pi * 60 * v90 = sin( 2 * pi * 90 * v120 = sin( 2 * pi * 120 v150 = sin( 2 * pi * 150 v180 = sin( 2 * pi * 180 v210 = sin( 2 * pi * 210 v240 = sin( 2 * pi * 240 ts ); ts ); ts ); * ts ); * ts ); * ts ); * ts ); * ts ); tsfast = (0:nsfast) * dtfast; v30fast = sin( 2 * pi * 30 * tsfast ); v60fast = sin( 2 * pi * 60 * tsfast ); v90fast = sin( 2 * pi * 90 * tsfast ); v120fast = sin( 2 * pi * 120 * tsfast ); v150fast = sin( 2 * pi * 150 * tsfast ); v180fast = sin( 2 * pi * 180 * tsfast ); v210fast = sin( 2 * pi * 210 * tsfast ); v240fast = sin( 2 * pi * 240 * tsfast ); % plot time domain figure( 1 ) subplot(4,2,1) plot( ts, v30, ts, v30, 'o', tsfast, v30fast, ':' ) title('30/60/90/120 Hz sampled at 200 Hz') % xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,3) plot( ts, v60, ts, v60, 'o', tsfast, v60fast, ':' ) % title('60 HZ sine sampled at 200 Hz') % xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,5) plot( ts, v90, ts, v90, 'o', tsfast, v90fast, ':' ) % title('90 HZ sine sampled at 200 Hz') % xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,7) plot( ts, v120, ts, v120, 'o', tsfast, v120fast, ':' ) % title('120 HZ sine sampled at 200 Hz') xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,2) plot( ts, v150, ts, v150, 'o', tsfast, v150fast, ':' ) title('150/180/210/240 Hz sampled at 200 Hz') % xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,4) plot( ts, v180, ts, v180, 'o', tsfast, v180fast, ':' ) % title('180 HZ sine sampled at 200 Hz') % xlabel('Time (sec)') page 4 of 22 N06 % ylabel('Amplitude') subplot(4,2,6) plot( ts, v210, ts, v210, 'o', % title('210 HZ sine sampled at % xlabel('Time (sec)') % ylabel('Amplitude') subplot(4,2,8) plot( ts, v240, ts, v240, 'o', % title('240 HZ sine sampled at xlabel('Time (sec)') % ylabel('Amplitude') tsfast, v210fast, ':' ) 200 Hz') tsfast, v240fast, ':' ) 200 Hz') % plot frequency domain figure( 2 ) f0 = fs / 2; f = 0 : 400; fsym = [ 30 60 90 120 150 180 210 240 270 300 330 360 390 ]; a = f0 - abs( mod(f,fs) - f0 ); asym = f0 - abs( mod(fsym,fs) - f0 ); plot( f, a, fsym, asym, 'o' ) title( 'Aliasing = fs/2 - abs( mod(f,fs) - fs/2 )' ) xlabel( 'Actual frequency [Hz]' ) ylabel( 'Apparent frequency [Hz] for fs=200 Hz' ) page 5 of 22 N06 page 6 of 22 Nonrecursive Digital Filters analog filter (only real-time) VIN VOUT xi yi digital filter (real-time, post sampling and backward in time) Digital filter xi raw yi filtered Low pass smoothing with moving averages y i 1 / 2 x i 1 / 2 x i 1 first-order, low pass, backward difference y i 1/ 3 x i 1/ 3 x i 1 1/ 3 x i2 second-order, low pass, backward difference y i 1 / 4 x i 1 / 4 x i 1 1 / 4 x i 2 1 / 4 x i 3 order = number of time samples h third-order, low pass, backward difference e,g, y i 1/ 2 x i 1/ 2 x i2 is second order DC gain = sum of coefficients DC gain > 0 low pass (in general) DC gain = 0 high pass or bandpass (in general) N06 page 7 of 22 Backward difference y i 1 / 3 x i 1 / 3 x i 1 1 / 3 x i 2 a) b) c) d) all time steps are prior history has startup artifact OK for real-time phase lag e) finite impulse response (FIR) - no knowledge of events for t i t EVENT h * order actual impulse xi yi i spring-mass-damper (second order analog filter) has infinite impulse response (IIR) t Forward difference y i 1 / 3 x i 2 1 / 3 x i 1 1 / 3 x i a) b) c) d) e) all time steps in future has artifact at end of sequence NOT OK for real-time phase lead FIR N06 page 8 of 22 Central difference y i 1 / 3 x i 1 1 / 3 x i 1 / 3 x i 1 a) b) c) d) e) some time steps in future and some in prior history has artifact at startup and end of sequence NOT OK for real-time zero phase (generally) FIR Weighting y i 1 / 3 x i 1 1 / 3 x i 1 / 3 x i 1 uniform weighing y i 1 / 4 x i1 1 / 2 x i 1 / 4 x i 1 central weighing y i 1 / 2 x i1 1 / 4 x i 1 / 4 x i 1 forward weighing y i 1 / 4 x i1 1 / 4 x i 1 / 2 x i 1 backward weighing High pass y i x i x i 1 / h x first-order, first derivative, backward difference (actually at i-½) y i x i1 x i / h x first-order, first derivative, forward difference (actually at i+½) x x i1 / 2 x i1 / 2 / 2 x i1 x i / h y i x i 1 x i 1 / 2h x DC gain = 0 average second-order, first derivative, central difference high pass x x i1 / 2 x i1 / 2 / h x i1 x i / h x y i x i1 2x i x i1 / h 2 DC gain = 0 x i x i1 / h / 2 high pass x i x i1 / h / h second-order, second derivative, central difference N06 page 9 of 22 Plot the frequency response transfer function of the digital filter shown below that modifies the sequence xi into a new sequence yi. Circle any filter descriptors that apply. yi = xi+1 + 3 * xi + xi-1 recursive nonrecursive forward-difference central-difference backward-difference high pass band pass low pass FIR IIR forward-weighted centrally-weighted backward-weighted phase-lead zero-phase phase-lag startup artifact no startup artifact first-order second-order third-order DC gain = 0 DC gain = 1 DC gain = 5 +20 dB 0 dB -20 dB 0 f / fs = 0.25 f /fs = 0.50 f / fs = 0.75 f / fs = 1.00 N06 page 10 of 22 Plot the frequency response transfer function of the digital filter shown below that modifies the sequence xi into a new sequence yi. Circle any filter descriptors that apply. yi = xi+1 + 3 * xi + xi-1 recursive nonrecursive forward-difference central-difference backward-difference high pass band pass low pass FIR IIR forward-weighted centrally-weighted backward-weighted phase-lead zero-phase phase-lag startup artifact no startup artifact first-order second-order third-order DC gain = 0 DC gain = 1 DC gain = 5 N06 page 11 of 22 Frequency response for nonrecursive digital filters x e j 2 f t t i h i / fs r f / fs x i e j 2 r i y i x i1 3x i x i1 e j 2 r i1 3e j 2 r i e j 2 r i1 y i e j 2 r i e j 2 r 3 e j 2 r i e j 2 r i e j 2 r G y i / x i e j 2 r 3 e j 2 r for r f / f s 0 G 5 backward difference y i x i 3x i 1 x i 2 G 1 3e j 2 r e j 4 r N06 page 12 of 22 y(i) = x(i+1) + 3*x(i) + x(i-1) Gain [dB] 15 10 5 0 0 0.1 0.2 0.3 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0.7 0.8 0.9 1 Phase [deg] 1 0.5 0 -0.5 -1 0.4 0.5 0.6 Frequency ratio r = f/fs % nonrecursive.m - gain/phase for nonrecursive digital filter % HJSIII, 13.11.14 r = 0:0.01:1; G = exp(j*2*pi*r) + 3 + exp(-j*2*pi*r); % central difference %G = 1 + 3*exp(-j*2*pi*r) + exp(-j*4*pi*r); % backward difference amp = 20*log10( abs( G ) ); phi = 180/pi*angle( G ); subplot(2,1,1) plot( r,amp ) ylabel( 'Gain [dB]' ) title( 'y(i) = x(i+1) + 3*x(i) + x(i-1)') % title( 'y(i) = x(i) + 3*x(i-1) + x(i-2)') subplot(2,1,2) plot( r,phi ) ylabel( 'Phase [deg]' ) xlabel( 'Frequency ratio r = f/fs' ) N06 page 13 of 22 Backward difference y(i) = x(i) + 3*x(i-1) + x(i-2) Gain [dB] 15 10 5 0 0 0.1 0.2 0.3 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0.7 0.8 0.9 1 Phase [deg] 200 100 0 -100 -200 0.4 0.5 0.6 Frequency ratio r = f/fs N06 page 14 of 22 Startup artifact (works for recursive and nonrecursive) examples for second-order backward difference similar approach for end of sequence artifact a) ignore - start at i = 3 b) pad with constants - x 1 x 0 x 1 discontinuity in x , introduces high frequency artifact i -1 0 1 2 c) reflect x 0 x1 d 2 x1 x 2 x1 2x1 x 2 x 1 x1 d3 x1 x 3 x1 2x1 x 3 x1 0 artifact d2 d3 i -1 0 1 2 3 Filter forward and then backward in time (works for recursive and nonrecursive) 1) 2) 3) 4) filter forward in time reverse sequence of data (e.g. MATLAB "flipud" or "fliplr" filter again reverse sequence back to original can also perform using filter time loops a) effectively removes phase lag/lead b) provides double order attenuation 3 N06 page 15 of 22 Recursive Digital Filters Recursion compute a new value for a function using old values that were computed with the same function example - 6 ! 6 5 ! y i 1 / 3 x i 1 / 3 x i 1 1 / 3 y i 1 now includes y i 1 recursion provides IIR capability must be VERY careful with high pass filters Standard second-order backward-difference recursive form y i a 0 x i a 1 x i1 a 2 x i2 b1 y i1 b 2 y i2 DC gain = a 0 a 1 a 2 b1 b 2 Recursive digital filter to emulate spring-mass-damper analog filter VIN VOUT xi yi digital filter y x m y spring-mass-damper k c x N06 page 16 of 22 F my ky x cy x ky cx kx my cy digitize xi at fs = 1/h and use approximations for derivatives x i x i 2 / 2h x or x i x i1 / h y i y i 2 / 2h y or yi yi1 / h y y i 2y i1 y i2 / h 2 m m c c m c c 2 y i 2 2 y i 1 2 y i 2 y i y i 2 ky i x i x i 2 kx i 2h 2h h h h 2h 2h m m c m c c c k y i 2 2 y i 1 2 y i 2 k x i x i 2 2 2h 2h h h h 2h 2h m m c c c m c k y i k x i x i 2 2 2 y i 1 2 y i 2 2 2h 2h h h 2h 2h h ch kh 2 1 2m m ch kh 2 y i m 2m Butterworth = critical damping f ch 2 m n 2 c 2m 2 m f s fs ch ch x i x i 2 2y i 1 1 y i 2 2m 2m c 2m n n kh 2 2n f c 2 2 m fs fs 2 k 2f c m h 1 fs N06 page 17 of 22 standard second-order backward-difference recursive form y i a 0 x i a 1 x i1 a 2 x i2 b1 y i1 b 2 y i2 sample values for fc = 4 Hz, fs = 60 Hz A 1 2 fc fc 2 f s f s f f a 0 2 c 2 c fs fs 2 2 1.594339 /A 0.372781 0 a1 0 f a 2 2 c fs b1 2 / A f b 2 1 2 c fs / A -0.262729 -1.254439 / A 0.364490 Hand-made 4 Hz LP Butterworth at 60 Hz sampling 1 0.5 Log gain 0 -0.5 -1 -1.5 -2 -1 -0.5 0 0.5 Log Frequency [Hz] 1 1.5 2 N06 page 18 of 22 Recursive digital filter using z-transform Laplace transform of transfer function A 0 A 1s A 2 s 2 ...A k s k Y(s) G (s) X(s) B 0 B1s B 2 s 2 ...B k s k second order expansion for continuous variables A 0 x A1 x A 2 x B0 y B1 y B2 y Butterworth filter low-pass A0 = 1, Aother = 0 Butterworth filter high-pass Ak = 1, Aother = 0 Butterworth filter (low-pass or high-pass) Order k B0 B1 B2 1 1 1 2 1 1.4142136 1 3 1 2 2 4 1 2.6131259 3.4142136 corresponding z-transform H( z) B3 1 2.6131259 B4 B5 1 a 0 a 1 z 1 a 2 z 2 ...a k z k Y(z) 1 2 k X(z) 1 b1z b 2 z ... b k z expansion for digitized variables a 0 x i a 1 x i1 a 2 x i2 a k x ik y i b1 y i1 b 2 y i2 b k y ik standard second-order backward-difference recursive form y i a 0 x i a 1 x i1 a 2 x i2 b1 y i1 b 2 y i2 second-order coefficients a and b using bilinear transformation from s to z C = 1 / tan( fc / fs ) A = B0 + B1 C + B2 C2 a0 = ( A0 + A1 C + A2 C2 ) / A a1 = ( 2 A0 - 2 A2 C2 ) / A a2 = ( A0 - A1 C + A2 C2 ) / A b1 = ( 2 B0 - 2 B2 C2 ) / A b2 = ( B0 - B1 C + B2 C2 ) / A sample values for fc = 4 Hz, fs = 60 Hz 4.70463 29.78689 0.033571 0.067143 0.033571 -1.41898 0.553269 reference: Digital Signal Processing, W. Stanley, G. Dougherty and R. Dougherty; 2nd ed, Reston, 1984 NOTE: MATLAB butter, filter, and filtfilt interchange a-b terms from notation above. N06 page 19 of 22 Single transfer function for 10 Hz LP Butterworth 1 0 -2 -3 -4 -5 -1 -0.5 0 0.5 1 1.5 Log Frequency [Hz] 2 2.5 3 Average transfer function for 10 Hz LP Butterworth 1 0 -1 Log gain Log gain -1 -2 -3 -4 -5 -1 -0.5 0 0.5 1 1.5 Log Frequency [Hz] 2 2.5 3 N06 % check_filter.m - check transfer function of digital filter % HJSIII, 13.11.14 clear % test conditions - 1024 points sampled at 1 KHz n = 1024; fs = 1000; f0 = fs / 2; dt = 1 / fs; t = dt * (0:(n-1)); % create second order Butterworth filter - cutoff 100 Hz fc = 10; wn = fc / f0; [ b, a ] = butter( 2, wn ); % frequencies for FFT df = fs / n; f = df * (0:(n-1)); f(1) = 1e-20; lf = log10( f ); % buffer to accumulate average/max/min transfer functions ave_trf = zeros( n, 1 ); max_trf = -100* ones( n, 1 ); min_trf = 100 * ones( n, 1 ); % repeat for multiple trials ntrial = 100; for itrial = 1:ntrial, % sample random signal xin = randn( n, 1 ); % apply filter xout = filter( b, a, xin ); % FFT fftin = fft( xin ); fftout = fft( xout ); % transfer function trf = fftout ./ fftin; mag_trf = log10( abs( trf ) ); ave_trf = ave_trf + mag_trf; max_trf = max( mag_trf, max_trf ); min_trf = min( mag_trf, min_trf ); % bottom of averaging loop end ave_trf = ave_trf / ntrial; figure( 1 ) plot( lf,mag_trf ) axis( [ -1 3 -5 1 ] ) title( 'Single transfer function for 10 Hz LP Butterworth' ) xlabel( 'Log Frequency [Hz]' ) ylabel( 'Log gain' ) figure( 2 ) plot( lf,ave_trf,'r', lf,max_trf,'g', lf,min_trf,'g' ) axis( [ -1 3 -5 1 ] ) title( 'Average transfer function for 10 Hz LP Butterworth' ) xlabel( 'Log Frequency [Hz]' ) ylabel( 'Log gain' ) page 20 of 22 N06 page 21 of 22 Recursive digital filter in MATLAB >> help butter BUTTER Butterworth digital and analog filter design. [B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator). The coefficients are listed in descending powers of z. The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn order [B,A] [B,A] [B,A] is a two-element vector, Wn = [W1 W2], BUTTER returns an 2N bandpass filter with passband W1 < W < W2. = BUTTER(N,Wn,'high') designs a highpass filter. = BUTTER(N,Wn,'low') designs a lowpass filter. = BUTTER(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. When used with three left-hand arguments, as in [Z,P,K] = BUTTER(...), the zeros and poles are returned in length N column vectors Z and P, and the gain in scalar K. When used with four left-hand arguments, as in [A,B,C,D] = BUTTER(...), state-space matrices are returned. BUTTER(N,Wn,'s'), BUTTER(N,Wn,'high','s') and BUTTER(N,Wn,'stop','s') design analog Butterworth filters. In this case, Wn is in [rad/s] and it can be greater than 1.0. See also buttord, besself, cheby1, cheby2, ellip, freqz, filter. N = order Wn = fC / fn = fC / (fS/2) NOTE: MATLAB butter, filter, and filtfilt interchange a-b terms from notation above. N06 >> help filter FILTER One-dimensional digital filter. Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the standard difference equation: a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na) If a(1) is not equal to 1, FILTER normalizes the filter coefficients by a(1). FILTER always operates along the first non-singleton dimension, namely dimension 1 for column vectors and non-trivial matrices, and dimension 2 for row vectors. [Y,Zf] = FILTER(B,A,X,Zi) gives access to initial and final conditions, Zi and Zf, of the delays. Zi is a vector of length MAX(LENGTH(A),LENGTH(B))-1, or an array with the leading dimension of size MAX(LENGTH(A),LENGTH(B))-1 and with remaining dimensions matching those of X. FILTER(B,A,X,[],DIM) or FILTER(B,A,X,Zi,DIM) operates along the dimension DIM. See also filter2 and, in the signal Processing Toolbox, filtfilt. Overloaded methods: timeseries/filter fxptui.filter dfilt.filter >> help filtfilt FILTFILT Zero-phase forward and reverse digital filtering. Y = FILTFILT(B, A, X) filters the data in vector X with the filter described by vectors A and B to create the filtered data Y. The filter is described by the difference equation: y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na) After filtering in the forward direction, the filtered sequence is then reversed and run back through the filter; Y is the time reverse of the output of the second filtering operation. The result has precisely zero phase distortion and magnitude modified by the square of the filter's magnitude response. Care is taken to minimize startup and ending transients by matching initial conditions. The length of the input x must be more than three times the filter order, defined as max(length(b)-1,length(a)-1). Note that FILTFILT should not be used with differentiator and Hilbert FIR filters, since the operation of these filters depends heavily on their phase response. See also filter. >> page 22 of 22