MATLAB Examples

# IIR Order 4 Filter

## Filter Coefficient in Floating Point

```a=[1.0000, -2.16860, 2.0097, -0.8766, 0.1505]; b=[0.0072, 0.00287, 0.0431, 0.0287, 0.0072]; ```

## Filter Coefficent in Fixed Point

```aFix = fi(-a,1,16,13); bFix = fi(b,1,16,13); ```

## Filter Coeffiecient in Fixed Point Hex Form

```aFixHex = aFix.hex bFixHex = bFix.hex ```
```aFixHex = e000 4565 bfb1 1c0d fb2f bFixHex = 003b 0018 0161 00eb 003b ```

## Input Signal to Filter

```Fs=8000; f1=200; f2=1449; t=0:1/Fs:60/f2; s1=sin(2*pi*f1*t); s2=sin(2*pi*f2*t); s=s1+s2; plot(t,s) ```

## Input Signal Spectrum and Filter Characteristic

```MyFFT(s,512,8000); ```
```freqz(b,a,2048,'half',8000) ```

## Write Header File for Code Composer Studio

```s_fix=fi(s,1,16,13); sd=str2double(s_fix.dec)'; fid=fopen('in.dat','w'); for i=1:length(sd) fprintf(fid,'%d',sd(i)); %fprintf(fid,'%s',','); fprintf(fid,'\n'); end fclose(fid); fid = fopen('InBuf.txt','w'); sHex = s_fix.hex; string = char(zeros(4*max(size(sd)),1)); j=1; for i=1:max(size(sHex)) if sHex(i)~=' ' string(j) = sHex(i); j = j+1; end end j=4; for i=1:max(size(string)) fprintf(fid,'%s',string(i)); if i==j fprintf(fid,'%s',','); fprintf(fid,'\n'); fprintf(fid,'%s','0x'); j = j+4; end end ```

## Filter Signal with MATLAB built-in

```y=filter(b,a,s); MyFFT(y,1024,8000); ```
```plot(t,y) ```

## Hex Form of the output Filter

```yFix = fi(y,1,16,15); yFixHex = yFix.hex ```
```yFixHex = 0000 00fb 0381 0c10 1d7e 3090 38d3 336c 2ccd 346e 4b7c 6290 69d1 6198 58d5 5b4d 6418 63d2 5303 3b37 2c32 29d1 27c5 181c fbd1 e212 d71b d677 d0a5 bd7e a60b 9a59 9f15 a859 a7a0 9cbf 961d 9fed b5ea c81f cd31 ccf2 d6f7 f01c 0c7c 1cd0 1fbe 22ff 3300 4c0e 5e48 5fb6 579c 55cc 6024 6ba2 6928 5715 4345 3b27 3c5e 37cc 2375 0677 f132 eaaa e8d9 dcbb c42f acd1 a490 a9cc ad87 a500 965c 919a 9e06 b1b5 bcec bc38 bc35 ca9a e5fc ff0d 09ff 0bc8 13f3 2a22 44c7 53d5 5306 4e7e 546c 6458 6f87 69fd 5864 4aaa 499e 4ce5 44e7 2d36 120b 0273 ff3c fa76 e80e cc46 b72b b25d b63a b312 a3a4 936f 9184 9f1b ae32 b1c4 accd aea4 c0ef dc26 eff8 f5e2 f851 0620 20e2 3aa4 45b1 4417 44e9 5265 65cf 6f07 6719 583d 51ef 56ff 5a6c 4ea8 3593 1e99 151d 13a3 0a96 f2cb d646 c507 c2e0 c36d b8d5 a431 948e 95a9 a277 ab19 a74d 9ffe a4c9 b9b1 d238 df98 e194 e65d f9b2 16cc 2d7d 348a 33f8 3b53 4f48 63dc 6a16 6126 572f 58c4 624a 6430 5521 3d06 2c14 2843 26b7 18bc fd42 e284 d5f3 d508 d09f bef4 a755 99fd 9d7f a75d a860 9e59 96b3 9ed1 b467 c7f7 ce93 ce43 d6af ee8f 0b76 1d81 2159 23a7 31f1 4a84 5e0b 610e 58f8 5599 5e9d 6a8d 69c7 58af 43fe 3a28 3acf 377c 24c1 07de f112 e929 e7b6 dd48 c5c8 ad9c a3a0 a839 ad24 a641 97cc 918e 9c8c b084 bd66 bdd0 bd10 c9bb e465 fe97 0b33 0d40 13fb 28b1 438a 543c 549a 4f6a 539e 62bf 6eff 6b24 59e4 4ac5 4836 4b9c 453b 2ec7 1307 01b5 fda2 f9db e927 cdcc b75a b0fe b4e5 b353 a52f 947a 90d8 9d80 ad85 b2ce ae59 aee6 bf9b dac6 f026 f767 f96b 0586 1f48 39e5 46ab 45a8 453e 511c 6466 6f20 6898 5965 5168 5566 599c 4f92 3728 1f01 13e0 1231 0a9c f442 d77b c493 c149 c28c b9af a5c9 950a 9479 a0fd ab0b a8bc a140 a468 b81e d147 e061 e32d e6eb f890 154a 2d5c 35f0 3545 3b05 4db9 62dc ```