C  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
C     SUBROUTINE FOR SMOOTHED SPECTRA BY PARZEN'S SPECTRAL WINDOW       
C  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
C                                                                       
C                                               CODED BY Y.OHSAKI       
C                                                                       
C     PURPOSE                                                           
C       TO SMOOTH FOURIER AND POWER SPECTRA BY APPLICATION OF           
C       PARZEN'S SPECTRAL WINDOW WITH THE SPECIFIED BANDWIDTH           
C       IN FREQUENCY DOMAIN                                             
C                                                                       
C     USAGE                                                             
C       CALL SWIN(NFOLD,F,G,ND,IND,DF,BAND)                             
C                                                                       
C     DESCRIPTION OF ARGUMENTS                                          
C       NFOLD - TOTAL NUMBER OF THE GIVEN SPECTRAL VALUES               
C       F(ND) - THE ORIGINAL/SMOOTHED FOURIER SPECTRUM AT CALL/RETURN   
C       G(ND) - THE ORIGINAL/SMOOTHED POWER SPECTRUM AT CALL/RETURN     
C       IND   - 100 WHEN FOURIER SPECTRUM IS ORIGINALLY GIVEN           
C               010 WHEN POWER SPECTRUM IS ORIGINALLY GIVEN             
C       DF    - FREQUENCY INCREMENT IN THE SPECTRUM IN HZ               
C       BAND  - THE SPECIFIED BANDWIDTH IN HZ                           
C                                                                       
C     REMARKS                                                           
C       THE RATIO BAND/DF MUST BE LARGER THAN 560/151, AND LESS THAN    
C       14000/151 OR 140(NFOLD-1)/151 WHICHEVER SMALLER                 
C                                                                       
C     SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED                     
C       NONE                                                            
C                                                                       
      SUBROUTINE SWIN(NFOLD,F,G,ND,IND,DF,BAND)                         
C                                                                       
      DIMENSION F(ND),G(ND),W(101),G1(4497),G2(4497)                    
C                                                                       
C     INITIALIZATION                                                    
C                                                                       
      T=1./DF                                                           
      IF(BAND.EQ.0.) GO TO 120                                          
      UDF=1.854305/BAND*DF                                              
      IF(UDF.GT.0.5) GO TO 230                                          
      LMAX=IFIX(2./UDF)+1                                               
      IF(LMAX.GT.101) GO TO 240                                         
C                                                                       
C     SPECTRAL WINDOW                                                   
C                                                                       
      W(1)=0.75*UDF                                                     
      DO 110 L=2,LMAX                                                   
      DIF=1.570796*REAL(L-1)*UDF                                        
      W(L)=W(1)*(SIN(DIF)/DIF)**4                                       
  110 CONTINUE                                                          
C                                                                       
C     CONVERSION FROM FOURIER TO POWER SPECTRUM                         
C                                                                       
  120 IF(IND.NE.100) GO TO 140                                          
      G(1)=F(1)**2/T                                                    
      DO 130 K=2,NFOLD-1                                                
      G(K)=2.*F(K)**2/T                                                 
  130 CONTINUE                                                          
      G(NFOLD)=F(NFOLD)**2/T                                            
C                                                                       
C     SMOOTHING OF POWER SPECTRUM                                       
C                                                                       
  140 IF(BAND.EQ.0.) GO TO 210                                          
      LL=LMAX*2-1                                                       
      LN=LL-1+NFOLD                                                     
      LT=(LL-1)*2+NFOLD                                                 
      LE=LT-LMAX+1                                                      
      DO 150 K=1,LT                                                     
      G1(K)=0.                                                          
  150 CONTINUE                                                          
      DO 160 K=1,NFOLD                                                  
      G1(LL-1+K)=G(K)                                                   
  160 CONTINUE                                                          
      DO 180 K=LMAX,LE                                                  
      S=W(1)*G1(K)                                                      
      DO 170 L=2,LMAX                                                   
      S=S+W(L)*(G1(K-L+1)+G1(K+L-1))                                    
  170 CONTINUE                                                          
      G2(K)=S                                                           
  180 CONTINUE                                                          
      DO 190 L=2,LMAX                                                   
      G2(LL+L-1)=G2(LL+L-1)+G2(LL-L+1)                                  
      G2(LN-L+1)=G2(LN-L+1)+G2(LN+L-1)                                  
  190 CONTINUE                                                          
      DO 200 K=1,NFOLD                                                  
      G(K)=G2(LL-1+K)                                                   
  200 CONTINUE                                                          
C                                                                       
C     SMOOTHED FOURIER SPECTRUM                                         
C                                                                       
  210 F(1)=SQRT(G(1)*T)                                                 
      DO 220 K=2,NFOLD-1                                                
      F(K)=SQRT(G(K)*T/2.)                                              
  220 CONTINUE                                                          
      F(NFOLD)=SQRT(G(NFOLD)*T)                                         
      RETURN                                                            
C                                                                       
  230 WRITE(6,601)                                                      
      STOP                                                              
  240 WRITE(6,602)                                                      
      STOP                                                              
C                                                                       
C     FORMAT STATEMENTS                                                 
C                                                                       
  601 FORMAT('BANDWIDTH IS TOO NARROW')                                 
  602 FORMAT('BANDWIDTH IS TOO WIDE')                                   
      END                                                               
