C REMEZFG.FOR FRONT-END FOR REMEZ FILTER DESIGN PROGRAM
C
C This is an example of the FORTRAN code required for interfacing
C a .MEX file to MATLAB.
C
C The calling syntax from an M file is:
C
C		h = remezf(nfilt, edge, fx, wtx, jtype)
C
C
C Marc Ullman  June 22, 1987
C Loren Shure  Oct. 5, 1987 revised
C Copyright (C)  The Mathworks Inc.
C All Rights Reserved
C
C This subroutine is the main gateway to MATLAB.  When a MEX function
C  is executed MATLAB calls the USRFCN subroutine in the corresponding
C  MEX file.
C
C DO NOT modify this subroutine declaration.

      SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
      INTEGER NLHS, NRHS
      INTEGER PLHS(*), PRHS(*)

      INTEGER*4 MXCREATEFULL, MXGETPR, MXGETM, MXGETN
      REAL*8	MXGETSCALAR

C User should modify the following code to fit his requirements.

      INTEGER NFILT,JTYPE, NBANDS, LGRID, NFCNS
      INTEGER EDGE, FX, WTX, H
      REAL*8 EP(20), FP(10), WP(10), HP(66)
      INTEGER M,N,MM,NN

      IF (NRHS .NE. 5) THEN
        CALL MEXERRMSGTXT('REMEZF requires five input arguments')
      ELSEIF (NLHS .NE. 1) THEN
        CALL MEXERRMSGTXT('REMEZF requires one output argument')
      ENDIF

      NFILT = INT(MXGETSCALAR(PRHS(1)))

      JTYPE = INT(MXGETSCALAR(PRHS(5)))

      M = MXGETM(PRHS(3))
      N = MXGETN(PRHS(3))
      NBANDS = MAX(M,N)
      IF (MIN(M,N) .NE. 1) THEN
        CALL MEXERRMSGTXT('REMEZF requires vector inputs')
      ENDIF

      LGRID = 16

      MM = MXGETM(PRHS(4))
      NN = MXGETN(PRHS(4))
      IF ((MM .NE. M) .OR. (NN .NE. N)) THEN
        CALL MEXERRMSGTXT(
     x 'REMEZF requires that FX and WTX be consistent')
      ENDIF

      MM = MXGETM(PRHS(2))
      NN = MXGETN(PRHS(2))
      IF ((MM .NE. M*2) .AND. (NN .NE. N*2)) THEN
        CALL MEXERRMSGTXT(
     x 'REMEZF requires that EDGE be consistent with FX')
      ENDIF

C	PUT RHS INTO POINTERS

      EDGE = MXGETPR(PRHS(2))
      FX   = MXGETPR(PRHS(3))
      WTX  = MXGETPR(PRHS(4))

C	PUT POINTERS INTO FORTRAN ARRAYS

      CALL MXCOPYPTRTOREAL8(EDGE,EP,MM*NN)
      MM = MXGETM(PRHS(3))
      NN = MXGETN(PRHS(3))
      CALL MXCOPYPTRTOREAL8(FX,FP,MM*NN)
      MM = MXGETM(PRHS(4))
      NN = MXGETN(PRHS(4))
      CALL MXCOPYPTRTOREAL8(WTX,WP,MM*NN)

      NFCNS = NFILT/2
      IF((NFILT - 2*NFCNS).EQ.1) NFCNS = NFCNS + 1
      PLHS(1) = MXCREATEFULL(NFCNS, 1, 0)
      H = MXGETPR(PLHS(1))

      CALL REMEZF (NFILT, JTYPE, NBANDS, LGRID, 
     1		EP, FP, WP, HP)

      CALL MXCOPYREAL8TOPTR(HP, H, NFCNS)

      RETURN
      END
