1、 实验用Matlab基本函数 1.abs Absolute value (magnitude) Syntax y = abs(x) Description y = abs(x) returns the absolute value of the elements of x. If x is complex, abs returns the complex modulus (magnitude). abs(x) = sqrt(real(x).^2 + imag(x).^2) If x is a MATLAB string, abs returns the numer
2、ic values of the ASCII characters in the string. The display format of the string changes; the internal representation does not. The abs function is part of the standard MATLAB language. Example Calculate the magnitude of the FFT of a sequence. t = (0:99)/100; % time v
3、ector x = sin(2*pi*15*t) + sin(2*pi*40*t); % signal y = fft(x); % compute DFT of x m = abs(y); % magnitude Plot the magnitude. f = (0:length(y)-1)'/length(y)*100; % frequency vector plot(f,m) 2. angle Phase angle Syntax p = angle(h)
4、 Description p = angle(h) returns the phase angles, in radians, of the elements of complex vector or array h. The phase angles lie between - and . For complex sequence h = x + iy = meip, the magnitude and phase are given by m = abs(h) p = angle(h) To convert to the original h from its mag
5、nitude and phase, type i = sqrt(-1) h = m.*exp(i*p) The angle function is part of the standard MATLAB language. Example Calculate the phase of the FFT of a sequence. t = (0:99)/100; % time vector x = sin(2*pi*15*t) + sin(2*pi*40*t); % signal y = fft(x);
6、 % compute DFT of x p = unwrap(angle(y)); % phase Plot the phase. f = (0:length(y)-1)'/length(y)*100; % frequency vector plot(f,p) Algorithm angle can be expressed as angle(x) = imag(log(x)) = atan2(imag(x),real(x)) 3. besself Bessel analog filter design
7、 Syntax [b,a] = besself(n,Wn) [b,a] = besself(n,Wn,'ftype') [z,p,k] = besself(...) [A,B,C,D] = besself(...) Description besself designs lowpass, bandpass, highpass, and bandstop analog Bessel filters. Analog Bessel filters are characterized by almost constant group delay across the entire
8、 passband, thus preserving the wave shape of filtered signals in the passband. Digital Bessel filters do not retain this quality, and besself therefore does not support the design of digital Bessel filters. [b,a] = besself(n,Wn) designs an order n lowpass analog filter with cutoff frequency Wn. I
9、t returns the filter coefficients in the length n+1 row vectors b and a, with coefficients in descending powers of s, derived from the transfer function Cutoff frequency is the frequency at which the magnitude response of the filter begins to decrease significantly. For besself, the cutoff freque
10、ncy Wn must be greater than 0. The magnitude response of a Bessel filter designed by besself is always less than at the cutoff frequency, and it decreases as the order n increases. If Wn is a two-element vector, Wn = [w1 w2] with w1 < w2, then besself(n,Wn) returns an order 2*n bandpass analog f
11、ilter with passband w1 <ω< w2. [b,a] = besself(n,Wn,'ftype') designs a highpass or bandstop filter, where the string 'ftype' is: l 'high' for a highpass analog filter with cutoff frequency Wn l 'stop' for an order 2*n bandstop analog filter if Wn is a two-element vector, Wn = [w1 w2] The
12、 stopband is w1 <ω< w2. With different numbers of output arguments, besself directly obtains other realizations of the analog filter. To obtain zero-pole-gain form, use three output arguments as shown below. [z,p,k] = besself(n,Wn) or [z,p,k] = besself(n,Wn,'ftype') returns the zeros and po
13、les in length n or 2*n column vectors z and p and the gain in the scalar k. Algorithm besself performs a four-step algorithm: l It finds lowpass analog prototype poles, zeros, and gain using the besselapfunction. l It converts the poles, zeros, and gain into state-space form. l It transform
14、s the lowpass filter into a bandpass, highpass, or bandstop filterwith desired cutoff frequencies using a state-space transformation. l It converts the state-space filter back to transfer function or zero-pole-gain form, as required. 4. bilinear Bilinear transformation method for analog-to-digi
15、tal filter conversion Syntax [zd,pd,kd] = bilinear(z,p,k,fs) [zd,pd,kd] = bilinear(z,p,k,fs,Fp) [numd,dend] = bilinear(num,den,fs) [numd,dend] = bilinear(num,den,fs,Fp) [Ad,Bd,Cd,Dd] = bilinear(A,B,C,D,fs) [Ad,Bd,Cd,Dd] = bilinear(A,B,C,D,fs,Fp) Description The bilinear transformation i
16、s a mathematical mapping of variables. In digital filtering, it is a standard method of mapping the s or analog plane into the z or digital plane. It transforms analog filters, designed using classical filter design techniques, into their discrete equivalents. Zero-Pole-Gain [zd,pd,kd] = bilinea
17、r(z,p,k,fs) and [zd,pd,kd] = bilinear(z,p,k,fs,Fp) convert the s-domain transfer function specified by z, p, and k to a discrete equivalent. Inputs z and p are column vectors containing the zeros and poles, k is a scalar gain, and fs is the sampling frequency in hertz. bilinear returns the discre
18、te equivalent in column vectors zd and pd and scalar kd. Fp is the optional match frequency, in hertz, for prewarping. Transfer Function [numd,dend] = bilinear(num,den,fs) and [numd,dend] = bilinear(num,den,fs,Fp) convert an s-domain transfer function given by num and den to a discrete equiva
19、lent. Row vectors num and den specify the coefficients of the numerator and denominator, respectively, in descending powers of s. fs is the sampling frequency in hertz. bilinear returns the discrete equivalent in row vectors numd and dend in descending powers of z (ascending powers of z-1). Fp is
20、 the optional match frequency, in hertz, for prewarping. 5. butter Butterworth analog and digital filter design Syntax [b,a] = butter(n,Wn) [b,a] = butter(n,Wn,'ftype') [b,a] = butter(n,Wn,'s') [b,a] = butter(n,Wn,'ftype','s') [z,p,k] = butter(...) [A,B,C,D] = butter(...) Description
21、 butter designs lowpass, bandpass, highpass, and bandstop digital and analog Butterworth filters. Butterworth filters are characterized by a magnitude response that is maximally flat in the passband and monotonic overall. Butterworth filters sacrifice rolloff steepness for monotonicity in the pa
22、ss- and stopbands. Unless the smoothness of the Butterworth filter is needed, an elliptic or Chebyshev filter can generally provide steeper rolloff characteristics with a lower filter order. Digital Domain [b,a] = butter(n,Wn) designs an order n lowpass digital Butterworth filter with cutoff fre
23、quency Wn. It returns the filter coefficients in length n+1 row vectors b and a, with coefficients in descending powers of z. Cutoff frequency is that frequency where the magnitude response of the filter is . For butter, the normalized cutoff frequency Wn must be a number between 0 and 1, where
24、1 corresponds to the Nyquist frequency, π radians per sample. If Wn is a two-element vector, Wn = [w1 w2], butter returns an order 2*n digital bandpass filter with passband w1 <ω< w2. [b,a] = butter(n,Wn,'ftype') designs a highpass or bandstop filter, where the string 'ftype' is either: l '
25、high' for a highpass digital filter with cutoff frequency Wn l 'stop' for an order 2*n bandstop digital filter if Wn is a two-element vector, Wn = [w1 w2]. The stopband is w1 <ω< w2. With different numbers of output arguments, butter directly obtains other realizations of the filter. To obtain
26、zero-pole-gain form, use three output arguments as shown below. [z,p,k] = butter(n,Wn) or [z,p,k] = butter(n,Wn,'ftype') returns the zeros and poles in length n column vectors z and p, and the gain in the scalar k. Analog Domain [b,a] = butter(n,Wn,'s') designs an order n lowpass analog Bu
27、tterworth filter with cutoff frequency Wn rad/s. It returns the filter coefficients in the length n+1 row vectors b and a, in descending powers of s, derived from the transfer function butter's cutoff frequency Wn must be greater than 0 rad/s. If Wn is a two-element vector with w1 < w2, butter
28、n,Wn,'s') returns an order 2*n bandpass analog filter with passband w1 <ω< w2. [b,a] = butter(n,Wn,'ftype','s') designs a highpass or bandstop filter. With different numbers of output arguments, butter directly obtains other realizations of the analog filter. To obtain zero-pole-gain form, us
29、e three output arguments as shown below. [z,p,k] = butter(n,Wn,'s') or [z,p,k] = butter(n,Wn,'ftype','s') returns the zeros and poles in length n or 2*n column vectors z and p and the gain in the scalar k. Examples Example 1 For data sampled at 1000 Hz, design a 9th-order highpass Butterw
30、orth filter with cutoff frequency of 300 Hz. [b,a] = butter(9,300/500,'high'); The filter's frequency response is freqz(b,a,128,1000) Example 2 Design a 10th-order bandpass Butterworth filter with a passband from 100 to 200 Hz and plot its impulse response, or unit sample response. n =
31、 5; Wn = [100 200]/500; [b,a] = butter(n,Wn); [y,t] = impz(b,a,101); stem(t,y) 6. cheby1 Chebyshev Type I filter design (passband ripple) Syntax [b,a] = cheby1(n,Rp,Wn) [b,a] = cheby1(n,Rp,Wn,'ftype') [b,a] = cheby1(n,Rp,Wn,'s') [b,a] = cheby1(n,Rp,Wn,'ftype','s') [z,p,k] = cheby1(...
32、) [A,B,C,D] = cheby1(...) Description cheby1 designs lowpass, bandpass, highpass, and bandstop digital and analog Chebyshev Type I filters. Chebyshev Type I filters are equiripple in the passband and monotonic in the stopband. Type I filters roll off faster than type II filters, but at the expe
33、nse of greater deviation from unity in the passband. Digital Domain [b,a] = cheby1(n,Rp,Wn) designs an order n Chebyshev lowpass digital Chebyshev filter with cutoff frequency Wn and Rp dB of peak-to-peak ripple in the passband. It returns the filter coefficients in the length n+1 row vectors b
34、and a, with coefficients in descending powers of z. Cutoff frequency is the frequency at which the magnitude response of the filter is equal to -Rp dB. For cheby1, the cutoff frequency Wn is a number between 0 and 1, where 1 corresponds to the Nyquist frequency, π radians per sample. Smaller valu
35、es of passband ripple Rp lead to wider transition widths (shallower rolloff characteristics). If Wn is a two-element vector, Wn = [w1 w2], cheby1 returns an order 2*n bandpass filter with passband w1 <ω< w2. [b,a] = cheby1(n,Rp,Wn,'ftype') designs a highpass or bandstop filter, where the strin
36、g 'ftype' is either: l 'high' for a highpass digital filter with cutoff frequency Wn l 'stop' for an order 2*n bandstop digital filter if Wn is a two-element vector, Wn = [w1 w2] The stopband is w1 <ω< w2. With different numbers of output arguments, cheby1 directly obtains other realizati
37、ons of the filter. To obtain zero-pole-gain form, use three output arguments as shown below. [z,p,k] = cheby1(n,Rp,Wn) or [z,p,k] = cheby1(n,Rp,Wn,'ftype') returns the zeros and poles in length n column vectors z and p and the gain in the scalar k. Analog Domain [b,a] = cheby1(n,Rp,Wn,'s')
38、 designs an order n lowpass analog Chebyshev Type I filter with cutoff frequency Wn rad/s. It returns the filter coefficients in length n+1 row vectors b and a, in descending powers of s, derived from the transfer function Cutoff frequency is the frequency at which the magnitude response of the f
39、ilter is -Rp dB. For cheby1, the cutoff frequency Wn must be greater than 0 rad/s. If Wn is a two-element vector Wn = [w1 w2] with w1 < w2, then cheby1(n,Rp,Wn,'s') returns an order 2*n bandpass analog filter with passband w1 <ω< w2. [b,a] = cheby1(n,Rp,Wn,'ftype','s') designs a highpass or ba
40、ndstop filter. You can supply different numbers of output arguments for cheby1 to directly obtain other realizations of the analog filter. To obtain zero-pole-gain form, use three output arguments as shown below. [z,p,k] = cheby1(n,Rp,Wn,'s') or [z,p,k] = cheby1(n,Rp,Wn,'ftype','s') returns
41、 the zeros and poles in length n or 2*n column vectors z and p and the gain in the scalar k. Examples Example 1: Lowpass Filter For data sampled at 1000 Hz, design a 9th-order lowpass Chebyshev Type I filter with 0.5 dB of ripple in the passband and a cutoff frequency of 300 Hz. [b,a] = cheb
42、y1(9,0.5,300/500); The frequency response of the filter is freqz(b,a,512,1000) Example 2: Bandpass Filter Design a 10th-order bandpass Chebyshev Type I filter with a passband from 100 to 200 Hz and plot its impulse response. n = 10; Rp = 0.5; Wn = [100 200]/500; [b,a] = cheby1(n,Rp,Wn);
43、 [y,t] = impz(b,a,101); stem(t,y) 7. conv Convolution and polynomial multiplication Syntax c = conv(a,b) Description c = conv(a,b) convolves vectors a and b. The convolution sum is where N is the maximum sequence length. The series is indexed from n + 1 and k + 1 instead of the usual
44、 n and k because MATLAB vectors run from 1 to n instead of from 0 to n-1. The conv function is part of the standard MATLAB language. Example The convolution of a = [1 2 3] and b = [4 5 6] is c = conv(a,b) c = 4 13 28 27 18 8. ellip Elliptic (Cauer) filter design Syntax
45、 [b,a] = ellip(n,Rp,Rs,Wn) [b,a] = ellip(n,Rp,Rs,Wn,'ftype') [b,a] = ellip(n,Rp,Rs,Wn,'s') [b,a] = ellip(n,Rp,Rs,Wn,'ftype','s') [z,p,k] = ellip(...) [A,B,C,D] = ellip(...) Description ellip designs lowpass, bandpass, highpass, and bandstop digital and analog elliptic filters. Elliptic fil
46、ters offer steeper rolloff characteristics than Butterworth or Chebyshev filters, but are equiripple in both the pass- and stopbands. In general, elliptic filters meet given performance specifications with the lowest order of any filter type. Digital Domain [b,a] = ellip(n,Rp,Rs,Wn) designs an o
47、rder n lowpass digital elliptic filter with cutoff frequency Wn, Rp dB of ripple in the passband, and a stopband Rs dB down from the peak value in the passband. It returns the filter coefficients in the length n+1 row vectors b and a, with coefficients in descending powers of z. The cutoff freque
48、ncy is the edge of the passband, at which the magnitude response of the filter is -Rp dB. For ellip, the cutoff frequency Wn is a number between 0 and 1, where 1 corresponds to half the sampling frequency (Nyquist frequency). Smaller values of passband ripple Rp and larger values of stopband attenua
49、tion Rs both lead to wider transition widths (shallower rolloff characteristics). If Wn is a two-element vector, Wn = [w1 w2], ellip returns an order 2*n bandpass filter with passband w1 <ω< w2. [b,a] = ellip(n,Rp,Rs,Wn,'ftype') designs a highpass or bandstop filter, where the string 'ftype' i
50、s either: l 'high' for a highpass digital filter with cutoff frequency Wn l 'stop' for an order 2*n bandstop digital filter if Wn is a two-element vector, Wn = [w1 w2]. The stopband is w1 <ω< w2. With different numbers of output arguments, ellip directly obtains other realizations of the fil






