reas_spectro.c

来自「Time-Frequency Toolbox,其中包含很常用的MATLAB程序」· C语言 代码 · 共 443 行 · 第 1/2 页

C
443
字号
/* EXISTS AN INTERFACE PROGRAM TO MATLAB : reasSP.C		      *
 *====================================================================*
 * Name of the function : reas_spectro   			      *
 * Author               : Manuel DAVY                                 *
 * Date of creation     : 15 - 03 - 1999                              *
 *--------------------------------------------------------------------*
 * THE ALGORITHM             				              *
 *								      *
 * Given a signal, a smoothing window and the time instants where to  *
 * compute, the reassigned and the classical spectrogram are computed.*
 * Furthermore, the reassignment field is given as an output. It is   *
 * possible, for certain applications, to weigh the field of          *
 * reassignment with a weighting_field.				      *
 *								      *
 * The metod consists in computing the 3 spectrograms of the signal   *
 * with the window, its derivative and the time times the window.     *
 *  STFT_H, STFT_DH, STFT_TH					      *
 *								      *
 *								      *
 * Then, the reassignment field is computed according to the classical*
 * formulas (in each time-frequency point) : 			      *
 *								      *
 *								      *
 *	       -              -					      *
 *	       |     STFT_TH  | 				      *
 *	       |  Re -------  | 				      *
 *	       |     STFT_H   |					      *
 *    Field =  | 	      |					      *
 *	       |     STFT_DH  |					      *
 *	       |  Im -------  |					      *
 *	       |     STFT_H   |					      *
 *	       -	      -					      *
 *								      *
 * Rmq : The subroutine 'stft' computes the Short time Fourier Transf.*
 *	 With a ponderation that depends on wether the window covers  *
 *	 only points of the signal or zeros padding points. It is     *
 *	 necessary to suppress this normalization here, because it    *
 *	 depends on the window (and then is different for e.g. the    *
 *	 derivative of the window)				      *
 *								      *
 *								      *
 * The additional weighting field is optional.			      *
 *====================================================================*
 * INPUT VARIABLES   					              *
 * Name              |                role               	      *
 * Signal            | Signal for which the reassigned spectro is     *
 *                   | computed                                       *
 * Signal.length     | Length of the input signal		      *
 * Signal.is_complex | Indicates wether the signal is complex valued  *
 *                   | or real_valued                                 *
 * Signal.real_part  | Real and part of the signal	              *
 * Signal.imag_part  | Imaginary part of the signal (only if          *
 *                   | 'Signal.is_complex == TRUE)                    *
 *                   |                                                *
 * Window            | The window 'h' in the STFT, with ODD length    *
 * Window_Length     | Length of the window			      *
 *                   |                                                *
 * weighting_field   | Optional weigthing field (matrix size equals   *
 *                   | the size of the reassigned spectrogram         *
 *use_weigthing_field| TRUE if the preceding field is employed        *
 * TFR_reassigned :  | tfr that is reassigned after the computation   *
 *   .N_time         | Number of positions for the smoothing window   *
 *                   | = Number of columns in the final reas. spectro *
 *   .N_freq         | Number of frequency bins in the reas. spectro  *
 *                   | = Number of rows in the final reas. spectro    *
 *   .freq_bins      | MUST BE ALLOCATED                              * 
 *   .time_instants  | time instants where the smoothing window is    *
 *                   | positioned (length = TFR_reassigned.N_time)    *
 *                   | its points must be REGULARLY spaced            *
 *   .is_complex     | must be set to FALSE here (the spectro is real *
 *                   | valued)                                        *
 * TFR_not_reassigned| final spectrogram (not reassigned)             *
 *   .N_time         | Number of positions for the smoothing window   *
 *                   | = Number of columns in the final spectrogram   *
 *   .N_freq         | Number of frequency bins in the spectrogram    *
 *                   | = Number of rows in the final spectrogram      * 
 *   .freq_bins      | MUST BE ALLOCATED                              * 
 *   .time_instants  | time instants where the smoothing window is    *
 *                   | positioned (length = TFR_not_reassigned.N_time)*
 *                   | its points must be REGULARLY spaced            *
 *   .is_complex     | must be set to FALSE here (the spectro is real *
 *                   | valued)                                        *
 *--------------------------------------------------------------------*
 * OUTPUT VARIABLES    						      *
 * Name              |                  role                	      *
 * TFR_reassigned :  | tfr that is reassigned after the computation   *
 *  .freq_bins       | vector of frequency bins                       *
 *  .real_part       | matrix of the reassigned spectrogram           *
 *                   |                                                *
 * TFR_not_reassigned| final spectrogram (not reassigned)             *
 *  .freq_bins       | vector of frequency bins                       *
 *  .real_part       | matrix of the reassigned spectrogram           *
 *                   |                                                *
 * field_time        |   Field of reassignement employed, decomposed  *
 * field_freq        |   on the basis vectors parallel to the time    *
 *                   |   axis and the freq. axis		      *
 *--------------------------------------------------------------------*
 * INTERNAL VARIABLES 						      *
 * Name              |                role                 	      *
 * normh             |   Vectors of the normalization used at each    *
 * normth            |   time instant in the computation of a STFT    *
 * normdt            |   size : TFR_reassigned.N_time		      *
 *                   |             				      *
 * twindow           |   The window times a vector of time	      *
 * dwindow           |   The derivative of the window		      *
 *                   |             				      *
 * stft_h            | the STFT of the signal computed with the       *
 * and its fields    | window 'window'			              * 
 *                   |             				      *
 * stft_th           | the STFT of the signal computed with the       *
 * and its fields    | window 'twindow'			              * 
 *                   |             				      *
 * stft_dh           | the STFT of the signal computed with the       *
 * and its fields    | window 'dwindow'			              * 
 *                   |             				      *
 * time, freq        |   current location in the TFR matrices         *
 * index             |   index of the preceding location in the vector*
 *                   |   containing the matrix.			      *
 *                   |             				      *
 * module            |   value of the spectrogram at the position     *
 *                   |   (time,freq)				      *
 *                   |             				      *
 * factor            | intermediary variable corresponding to a factor*
 *                   | of normalization of the reassignment field     *
 * step              | used to correct the derivative of the window at*
 *                   | its edges                                      *
 * step_time         | displacement step of the window                *
 * flag              | boolean variable used when a test concerning   *
 *                   | the input variables fails.
 *====================================================================*
 * SUBROUTINES USED HERE				      	      *
 *--------------------------------------------------------------------*
 * Name   | int idx(int line, int row, int nb_row)                    *
 * Action | computes the vector index for an element in a matrix given* 
 *        | the line and column indices and the number of columns     *
 * Place  | divers.c                                                  *
 *--------------------------------------------------------------------*
 * Name   | void stft (double *Sig_real, double *Sig_imag,            *
 *        |            int Signal_Length, double *Time_inst,          *
 *        |            int Time_inst_Length, int Nfft, double *Window,*
 *        |            int Window_Length, double *stft_real,          *
 *        |            double *stft_imag, double *norm_vector)        *
 * Action | Computes the Short time Fourier transform of a signal     *
 * Place  | stft.c						      *
 *--------------------------------------------------------------------*
 * Name   | void gradient (double *matrix, int size_x, int size_y,    *
 *        |             double step_x, double step_y, double *grad_x, * 
 *        |             double *grad_y)                               *
 * Action | Computes the gradient of a matrix			      *
 * Place  | gradient.c						      *
 *--------------------------------------------------------------------*
 * Nom    | double sqr(x)   				              *
 * Action | computes the square of x				      *
 * Lieu   | divers.c						      *
 *--------------------------------------------------------------------*
 * Name   | void reassign (double *TFR_to_reassign, double *field_x,  *
 *        |                double *field_y, int N_time, int N_freq,   *
 *        |	           double *TFR_reassigned)                    *
 * Action | moves the pixels in a TFR according to a field of reassig.*
 * Place  | reassign.c						      *
 *====================================================================*/

void
reas_spectro (type_signal Signal,
	      double *Window, int Window_Length,
	      type_TFR TFR_reassigned, type_TFR TFR_not_reassigned,
	      double *field_time, double *field_freq)

{
  /*-----------------------------------------------------------------*/
  /*                           VARIABLES                             */
  /*-----------------------------------------------------------------*/
  double        *normh, *normth, *normdh;
  double        *twindow, *dwindow;
  type_TFR       stft_h, stft_th, stft_dh;
  int            time, freq, index;
  double         module, step;
  double         step_time;
  unsigned char  flag;
  /*-----------------------------------------------------------------*/
  /*                       SOME TESTS ...                            */
  /*-----------------------------------------------------------------*/

 /*                         TFR_reassigned                           */

   if (TFR_reassigned.is_complex == TRUE)
    {
      printf ("reas_spectro.c : The reassigned tfr matrix must be real valued\n");
      exit(0);
    }
   if (TFR_reassigned.N_time <0)
    {
      printf ("reas_spectro.c : The field TFR_reassigned.N_time is not correctly set\n");
      exit(0);
    }
   if (TFR_reassigned.N_freq <0)
    {
      printf ("reas_spectro.c : The field TFR_reassigned.N_freq is not correctly set\n");
      exit(0);
    }
  if (TFR_reassigned.time_instants[0] <0)
    {
      printf ("reas_spectro.c : The field TFR_reassigned.time_instants is not correctly set\n");
      exit(0);
    }
 /*                       TFR_not_reassigned                         */

   if (TFR_not_reassigned.is_complex == TRUE)
    {
      printf ("reas_spectro.c : The tfr matrix must be real valued\n");
      exit(0);
    }
   if (TFR_not_reassigned.N_time <0)
    {
      printf ("reas_spectro.c : The field TFR_not_reassigned.N_time is not correctly set\n");
      exit(0);
    }
   if (TFR_not_reassigned.N_freq <0)
    {
      printf ("reas_spectro.c : The field TFR_not_reassigned.N_freq is not correctly set\n");
      exit(0);
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?