home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / dsp / 2049 < prev    next >
Encoding:
Text File  |  1992-08-30  |  5.8 KB  |  189 lines

  1. Newsgroups: comp.dsp
  2. Path: sparky!uunet!comp.vuw.ac.nz!newshost.wcc.govt.nz!kosmos.wcc.govt.nz!PROVO_B
  3. From: provo_b@kosmos.wcc.govt.nz (Bart)
  4. Subject: Re: Bugs in 68HC16 project.
  5. Message-ID: <1992Aug29.214543.24408@peponi.wcc.govt.nz>
  6. Sender: news@peponi.wcc.govt.nz (NEWS)
  7. Reply-To: provo_b@kosmos.wcc.govt.nz
  8. Organization: Wellington City Council, Wgtn, NZ
  9. Date: Sat, 29 Aug 1992 21:45:43 GMT
  10. Lines: 177
  11.  
  12. Subject: Re: Bugs in 68HC16 project
  13. >morrow@cns8.ucalgary.ca (Bill Morrow) writes on Wed, 26 Aug 92 17:07:29 GMT
  14.  
  15. >..HC16 development project 5-channel filter. His IIR filter seems to be 
  16. >  saturating, so I'm wondering if there is a mistake in the instructions.
  17.  
  18. YES!  There are also a number of areas in the code that could be improved.
  19.  
  20. The given SPI command queue causes PCS0 to be negated to eliminate the one
  21. "dont care" byte that would otherwise occur in an un-interrupted transmission.
  22. Normally this results in some "ghosting" on the display LEDs.  However if
  23. the PIT has been set for a faster Periodic Interrupt period, (by mistake
  24. or oversight) this ghosting will become brighter and can swamp the display!
  25.  
  26. The corrections to the coefficient calculations are below, and a short C
  27. program that I use to significantly ease the task of trying different
  28. filters is appended.
  29.  
  30. /*************************** extract ***************************************
  31. Subject: 'HC16 toolkit
  32. From: datwyler@javelin.sim.es.com (Doug Datwyler)
  33. Date: Tue, 17 Mar 1992 19:51:54 GMT
  34.  
  35. I got the question answered about the filter coefficients, twice. Once from
  36. an applications engineer, the other from Ross Wille. Thanks to both.
  37. On sheet 33 of the Project Manual, there is an error.
  38.  
  39. Fo = 1kHz  Fs = 24.95kHz Q = 1.5       Theta = ((2 * PI * Fo) / Fs)
  40. X = Theta / (2 * Q)                    if X > PI / 4 then X = 0.76398
  41.  
  42. Beta = 0.5 * {1 - tan(X)} / {1 + tan(X)}
  43. Gamma = (0.5 + Beta) * cos(Theta)
  44. Alpha = (0.5 - Beta) / 2
  45.  
  46. So, you see, the angle is never allowed to get where the Beta coefficient is
  47. less than 0 (zero).
  48. ****************************************************************************/
  49. /* Simple program to compute the coefficents for the 5band analyser program
  50.    and generate HC16 assembler lines for inclusion.  COEFF.exe compiled with
  51.    TURBO-C 1.5.
  52.  
  53.    Note that I have used the half bandwidth, h_bw = bw/2, for the program
  54.    input option and for the output lines.  This made it a little more
  55.    convenient in testing the 5band spectrum analyser.
  56.  
  57.    The following batch file generates coeff.asm, given the half bandwidth.
  58.    5bc.bat:
  59.             coeff 1000 , 3  >coeff.asm  | With this bandwidth, observed
  60.             coeff 2000 , 3 >>coeff.asm  | differences in peak frequencies
  61.             coeff 3000 , 3 >>coeff.asm  | could be attributed to the
  62.             coeff 4000 , 3 >>coeff.asm  | uncertainty of the frequency counter
  63.             coeff 5000 , 3 >>coeff.asm  | on the IWTASU CRO, which was used!
  64.  
  65.    The following batch file generates coeff.asm, given the filter's Q.
  66.    5bc0.bat:
  67.             coeff 125   .5  >coeff.asm
  68.             coeff 500  1.  >>coeff.asm
  69.             coeff 1000 1.5 >>coeff.asm
  70.             coeff 4000 1.0 >>coeff.asm
  71.             coeff 1E4   .5 >>coeff.asm
  72.  
  73.   Modify the 5band.asm file by removing the "dc.w <filter coefficient>" lines
  74.   and replace with:
  75.         ORG     $F0280          ; following lines have been replaced
  76.         INCLUDE 'COEFF.asm'     ; COEFF.asm created by 5bc0.bat from coeff.exe.
  77.         ....                    ; etc.
  78. */
  79.  
  80. /* -------- COEFF.c -------------- */
  81.  
  82. #include <stdio.h>
  83. #include <math.h>
  84.  
  85. void  PrintCoefficents(float Fo, float Q);
  86. float theta(float Fo, float Fs);
  87. float phi(float theta, float Q);
  88. float beta(float phi);
  89. float gamma(float beta, float theta);
  90. float alpha(float beta);
  91. int   FloatToInt(float X);
  92.  
  93. float Fs = 24950;         /* Fs is sample rate for acquired data */
  94.  
  95. void PrintCoefficents(float Fo,float Q)
  96. {
  97.   float h_bw,t,p,b,g,a ;  /* h_bw is half band-width of filter */
  98.  
  99.   h_bw = Fo/(2*Q) ;
  100.   t    = theta(Fo,Fs) ;
  101.   p    = phi(t,Q) ;
  102.   b    = beta(p) ;
  103.   g    = gamma(b,t) ;
  104.   a    = alpha(b) ;
  105.  
  106.   printf("  dc.W  $%04X    ; %5.0f Hz, gamma coeff,     Q = %8.2f\n",
  107.          FloatToInt(g),  Fo,  Q) ;
  108.   printf("  dc.W  $%04X    ; %5.0f Hz, -beta coeff, ±h_bw = %7.1f\n",
  109.          FloatToInt(-b), Fo, h_bw) ;
  110.   printf("  dc.W  $%04X    ; %5.0f Hz, alpha coeff.\n",
  111.          FloatToInt(a),  Fo) ;
  112. }
  113.  
  114. float theta(float Fo,float Fs)
  115. {
  116.   return ((2.0 * M_PI * Fo)/Fs);
  117. }
  118.  
  119.  
  120. float phi(float theta, float Q)
  121. {
  122.   float X;
  123.   X = theta/(2*Q);
  124.   if( X > M_PI/4 )  X = 0.76398;
  125.   return (X) ;
  126. }
  127.  
  128.  
  129. float beta(float X)
  130. {
  131.   float tanX;
  132.   tanX = tan(X);
  133.   return ( 0.5 * (1.0 - tanX)/(1.0 + tanX) );
  134. }
  135.  
  136.  
  137. float gamma(float beta,float theta)
  138. {
  139.   return (0.5 + beta) * cos(theta);
  140. }
  141.  
  142.  
  143. float alpha(float beta)
  144. {
  145.   return (0.5 - beta)/2.0;
  146. }
  147.  
  148.  
  149. int FloatToInt(float x)
  150. {
  151.   if ((x > 1.0) || (x < -1.0))
  152.     {
  153.       fprintf(stderr,"Cannot convert %f.\n",x);
  154.       return 0;
  155.     }
  156.   else return (int)(x * 32768.0);
  157. }
  158.  
  159.  
  160. void main(int argc, char *argv[])
  161. {
  162.   float Fo,Q,h_bw ;
  163.  
  164.   if (argc < 2)              /* argc is no. of arguments plus one */
  165.   {
  166.     printf("Usage: coeff Fo, [Q], [h_bw]   Note: Either Q or , h_bw\n") ;
  167.     exit(1) ;
  168.   }
  169.   sscanf(argv[1], "%f", &Fo) ;
  170.         if (*argv[2] != ',')
  171.          sscanf(argv[2], "%f", &Q ) ;
  172.         else
  173.          {
  174.          sscanf(argv[3], "%f", &h_bw) ;
  175.          Q = Fo/(2*h_bw) ;
  176.          }
  177.   PrintCoefficents( Fo, Q) ;
  178. }
  179. --------------------- end of COEFF.c ----------------------
  180.  
  181. Hope this helps,
  182. Regards,
  183. Bart.
  184. Bart Provo    |provo_b@kosmos.wcc.govt.nz |School of Electronic Engineering
  185. PO Box 47-105 |access via: PC/AT+modem on |Central Institute of Technology
  186. TRENTHAM      |home phone, (answer phone) |Private Bag 39-807
  187. Wellington    |     in NZ: (04)-5277-054  |Wellington Mail Centre,  NZ.
  188. NEW ZEALAND.  |     to NZ:+64-4-5277-054  |Phn:(04)5277-089 Fax:(04)5276-374
  189.