      SUBROUTINE FILQ(TQ,Q,NQ,DELT,PHCORR,P,RESPNS)   
C     
C     THIS SUBROUTINE APPLIES FILTERS TO THE DATA CONTAINED IN Q. 
C     ***** INPUT DATA *****  
C        NLOPAS = NUMBER OF TIMES A TWO POINT SUM FILTER IS APPLIED     
C        NHYPAS = NUMBER OF TIMES A TWO POINT DIFFERENCE FILTER IS
C                     APPLIED.
C     
      DIMENSION Q(1)
      DIMENSION RESPNS(26),P(26)    
      DOUBLE PRECISION TQ(1),QQ(50000)     
      CHARACTER LINE*40/' '/  
      PI=3.141592653    
      QUIST=2.0*DELT    
      READ *, NLOPAS, NHYPAS 
      PRINT 20,NLOPAS,NHYPAS,DELT   
   20 FORMAT(12X,9HNLOPAS = ,I5,/,12X,9HNHYPAS = ,I5,/,     
     *       12X,7HDELT = ,F13.3)   
      IF(NQ.GT.50000)THEN
        PRINT *,'Q IS > 50000 - TOO BIG FOR FILTERING' 
        STOP
      END IF
      DO 50 J=1,NQ
        QQ(J)=Q(J)
   50 CONTINUE    
      IF(NHYPAS.EQ.0)GO TO 200
      DO 100 J=1,NHYPAS 
        NQ = NQ -1
        ISKIP=0   
        DO 70 I=1,NQ    
          IF(ABS(TQ(I+1)-TQ(I)).GT.2.0*DELT)THEN
            ISKIP=ISKIP+1     
          ELSE    
            QQ(I-ISKIP)=QQ(I+1)-QQ(I)     
            TQ(I-ISKIP)=(TQ(I)+TQ(I+1))/2.0     
          END IF  
   70   CONTINUE  
        NQ=NQ-ISKIP     
  100 CONTINUE    
  200 IF(NLOPAS.EQ.0) GO TO 300     
      DO 290 J=1,NLOPAS 
        NQ = NQ -1
        ISKIP=0   
        DO 270 I=1,NQ   
          IF(ABS(TQ(I+1)-TQ(I)).GT.2.0*DELT)THEN
            ISKIP=ISKIP+1     
          ELSE    
            QQ(I-ISKIP)=QQ(I)+QQ(I+1)     
            TQ(I-ISKIP)=(TQ(I)+TQ(I+1))/2.0     
          END IF  
  270   CONTINUE  
        NQ=NQ-ISKIP     
  290 CONTINUE    
  300 PRINT 305,NQ
  305 FORMAT(12X,'NQ REDUCED TO           ',I13)   
      IF(NLOPAS.GT.0) THEN    
        XLOW=NLOPAS     
        XHY=NHYPAS
        Y=SQRT(XHY/XLOW)
        Y=ATAN(Y) 
      END IF
      A=0.0 
      B=0.0 
      PMAX=0.0    
      XMAX=-1.0   
      IF(NLOPAS.EQ.0)THEN     
        A=1.0     
        XMAX=(DELT*PI)/QUIST  
        PMAX=QUIST
      END IF
      IF(NHYPAS.EQ.0)THEN     
        B=1.0     
        XMAX=0.0  
        PMAX=9999.999   
      END IF
      IF(XMAX.EQ.-1.0)XMAX=Y  
      IF(PMAX.EQ.0.0)PMAX=(DELT*PI)/Y     
      IF(A.EQ.0.0)THEN  
        A=2.0*COS(XMAX) 
        A=A**NLOPAS     
      END IF
      IF(B.EQ.0.0)THEN  
        B=2.0*SIN(XMAX) 
        B=B**NHYPAS     
      END IF
      RMAX=A*B    
      PRINT 440,PMAX,RMAX     
  440 FORMAT(12X,'MAXIMUM RESPONSE AT P = ',F13.3,/,  
     *       12X,'MAX RESPONSE = ',1PE13.4,' FOR DIVIDING THE DATA'//)  
      PRINT 500   
  500 FORMAT(12X,6X,42('-'))  
      F=-0.02     
      DO 900 I=1,26     
        F=F+0.02  
        X=PI*F    
        IF(NLOPAS.GT.0)THEN   
          A=2.0*COS(X)  
          A=A**NLOPAS   
        END IF    
        IF(NLOPAS.EQ.0)A=1.0  
        IF(NHYPAS.GT.0)THEN   
          B=2.0*SIN(X)  
          B=B**NHYPAS   
        END IF    
        IF(NHYPAS.EQ.0)B=1.0  
        RESPNS(I)=A*B/RMAX    
        IF(F.GT.1.E-6)P(I)=DELT/F   
        IF(F.LT.1.E-6) P(I)=9999.999
        J=RESPNS(I)*39.0+1    
        LINE(J:J)='*'   
        PRINT 800,P(I),LINE   
  800   FORMAT(12X,F6.1,'I',A40,'I')
        LINE(J:J)=' '   
  900 CONTINUE    
      PRINT 500   
      PRINT 910   
  910 FORMAT(//12X,24HFILTER RESPONSE FUNCTION,/,16X,1HP,9X,8HRESPONSE) 
      DO 950 I=1,26     
        PRINT 920,P(I),RESPNS(I)    
  920 FORMAT(12X,F6.1,7X,1PE13.3)   
  950 CONTINUE    
      DO 960 I=1,NQ     
c       QQ(I)=QQ(I)/RMAX
  960 CONTINUE    
      PRINT 965,RMAX    
  965 FORMAT(/12X,'SCALED DATA DOWN BY RESPONSE MAXIMUM = ',
     *     1PE15.5)     
      L=MOD(NHYPAS,4)   
      PHCORR=-(L*PI)/2.0
      PRINT 970,PHCORR  
  970 FORMAT(//,12X,'APPLY PHASE CORRECTION OF ',F10.6,     
     *       ' TO ALL FUTURE PHASES',//)  
      DO 1000 J=1,NQ    
        Q(J)=QQ(J)
 1000 CONTINUE    
      RETURN
      END   
