      INCLUDE 'STDHDR.FOR'
      REAL xdata(0:maxv), ydata(0:maxv)
      REAL filtcoef(0:maxv), gain(0:maxv),  fp
      INTEGER i, n, k, dc, w, filtType
      LOGICAL BW


      DO i = 1, 100
        filtcoef(i) = 0.0
      END DO

      PRINT *, 'N  FP  DC  TYPE  Window  K'
      READ *, n, fp, dc, filtType, w, k
      !  n = 21:   fp = 0.25:   dc = 0: filtType = 0: w = 2: k = 200


      CALL FIRFreqSample(filtcoef, fp, n, dc, filtType, w)
      CALL WindowData(filtcoef, n, w)
      PRINT *, 'Writing results to FREQDATA.TXT'
      OPEN (2, FILE = 'FREQDATA.TXT', STATUS = 'UNKNOWN')
      DO  i = 0, n - 1
        WRITE (2,401) i, filtcoef(i)
      END DO
401   FORMAT (I2,2x,F10.5)
      CALL InitSEGraphics(-1, 'c:\FOR\LIB\*.FON')
      CALL BlackAndWhite(BW)
      DO i = 0, n - 1
        xdata(i) = i
        ydata(i) = filtcoef(i)
      END DO
      CALL SetCurrentWindow(2)
      CALL SetAxesType(0, 0)
      CALL ScalePlotArea(0.0, -0.2, 25.0, 0.61)
      CALL SetXYIntercepts(0.0, -0.2)
      IF (.NOT. BW)  CALL SetViewBackground(9)
      CALL SelectColor(15)
      CALL DrawYAxis(0.1, 0)
      CALL LabelYAxis(2, 0)
      CALL DrawXAxis(1.0, 0)
      CALL LabelXAxis(5, 0)
      CALL SetLineStyleXX(1, 1, 1)
      CALL DrawGridX(5)
      CALL DrawGridY(1)
      CALL SetLineStyleXX(0, 1, 1)

      CALL BarGraphData(xdata, ydata, n, 0.3, 2, 2)
      CALL SelectColor(15)
      CALL SetTextStyleXX(0, 0, 14, 16)
      CALL TitleWindow('FIR Filter Coefficients')
      CALL TitleXAxis('Filter Coefficient (n)')
      CALL TitleYAxis('Magnitude h(n)')
      CALL SetTextStyleXX(0, 0, 10, 12)
      READ(*,*)
      CALL ClearWindow


      CALL FreqResponse(filtcoef, ydata, n, k)
      DO i = 0, k
        xdata(i) = 0.5 * i / (k * 1.0)
        ydata(i) = ABS(ydata(i))
        gain(i) = ydata(i)
      END DO
      CALL SetCurrentWindow(3)
      IF (.NOT. BW) CALL SetViewBackground(1)
      CALL BorderCurrentWindow(4)
      CALL SetAxesType(0, 0)
      CALL ScalePlotArea(0.0, 0.0, 0.501, 1.21)
      CALL SetXYIntercepts(0.0, 0.0)
      CALL SelectColor(15)
      CALL DrawYAxis(.05, 0)
      CALL LabelYAxis(4, 0)
      CALL DrawXAxis(.1, 0)
      CALL LabelXAxis(1, 0)
      CALL SelectColor(9)
      CALL SetLineStyleXX(1, 1, 1)
      CALL DrawGridX(1)
      CALL DrawGridY(2)
      CALL SetLineStyleXX(0, 1, 1)
      CALL LinePlotData(xdata, ydata, k, 15, 0)
      CALL SelectColor(14)
      CALL TitleWindow('FIR Low Pass Filter Design')
      CALL TitleXAxis('Frequency in Hz')
      CALL TitleYAxis('Gain')

      DO i = 0, k
        ydata(i) = 20 * log10(ydata(i))
      END DO
      CALL SetCurrentWindow(4)
      IF (.NOT. BW) CALL SetViewBackground(1)
      CALL BorderCurrentWindow(4)
      CALL SetAxesType(0, 0)
      CALL ScalePlotArea(0.0, -100.0, 0.501, 20.01)
      CALL SetXYIntercepts(0.0, -100.0)
      CALL SelectColor(15)
      CALL DrawYAxis(10.0, 0)
      CALL LabelYAxis(2, 0)
      CALL DrawXAxis(0.1, 0)
      CALL LabelXAxis(1, 0)
      CALL SetLineStyleXX(1, 1, 1)
      CALL SelectColor(9)
      CALL DrawGridX(1)
      CALL DrawGridY(2)

      CALL LinePlotData(xdata, ydata, k, 15, 0)
      CALL SelectColor(14)
      CALL TitleWindow('FIR Low Pass Filter Design')
      CALL TitleXAxis('Frequency in Hz')
      CALL TitleYAxis('DB')

      WRITE (2,*)
      WRITE (2,*) 'Filter Response  '
      WRITE (2,*) 'Frequency          Gain        20*Log(Gain) '

      DO i = 0, k - 1
        xdata(i) = 0.5 * (i * 1.0) / (k * 1.0)
        WRITE(2,402) xdata(i), gain(i), ydata(i)
      END DO
402   FORMAT ( 3(F10.5, 2X))
      READ(*,*)
      CALL CloseSEGraphics
      CLOSE (2)

      END

