spectrumtxt

Capsim Block Documentation

Short Description

Spectrum probe (plots both frequency and time domain). Float/Int/Complex input buffers.

Top
Parameters
Num Description Type Name Default Value
0 Number of points ( dynamic window size ) int npts 128
1 Number of points to skip int skip 0
2 Plot title file title Spectrum
3 Linear = 0, dB = 1 int dbFlag 0
4 Window:0= Rec.,1=Hamm,2=Hann,3=Blackman int windFlag 0
5 Plot Style: 1=Line,2=Points,5=Bar Chart int plotStyleParam 1
6 Time Domain On/Off (1/0) int timeFlag 1
7 Sampling Rate (Bin Number if zero,Normalized if Negative float sampFreq 0
8 Control: 1=On, 0=Off int control 1
9 Buffer type:0= Float,1= Complex, 2=Integer int bufferType 0
10 0=Static,1=Dynamic int mode 0
Top
States
Num Type Name Initial Value Description
0 int numberInputBuffers
1 int numberOutputBuffers
2 float* xfreq_P
3 float* xTime_P
4 float* ypts_P
5 float* spect_P
6 int count 0
7 int totalCount 0
8 int blockOff 0
9 int bufi 0
10 int fftl
11 int fftexp
12 int plottedFlag
13 int displayed FALSE
Top

Declarations


 

	int	points;
	int samples;
	int numberPoints;
    	int i,j,ii;
	float tmp;
	complex val;
	float wind;
	char title1[80];
	char fname[80];
        char curveSubTitle[80];
	float* mag_P;
	float* phase_P;
	FILE *time_F;
	FILE *freq_F;



Top

Initialization Code



 

/* store as state the number of input/output buffers */
if((numberInputBuffers = NO_INPUT_BUFFERS()) <= 0) {
	fprintf(stderr,"spectrum: no inputs connected\n");
	return(1);
}
if(numberInputBuffers > 1) {
	fprintf(stderr,"spectrum: only one  input allowed \n");
	return(2);
}
if((numberOutputBuffers = NO_OUTPUT_BUFFERS()) > numberInputBuffers) {
	fprintf(stderr,"spectrum: too many outputs connected\n");
	return(3);
}
/*
 * Find closest power of two number 
 */
fftexp = (int) (log((float)npts)/log(2.0)+0.5);
fftl = 1 << fftexp;
if (fftl > npts ) {
	fftl = fftl/2;
	fftexp -= 1;
}
if (control && mode == DYNAMIC) {
	/*                                                      
	 * allocate arrays                                      
	 */                                                     
	xfreq_P = (float* )calloc(npts,sizeof(float));           
	xTime_P = (float* )calloc(npts,sizeof(float));           
	if( bufferType == COMPLEX_BUFFER)
		ypts_P = (float* )calloc(2*npts,sizeof(float));           
	else
		ypts_P = (float* )calloc(npts,sizeof(float));           
	spect_P = (float* )calloc(2*fftl,sizeof(float));           
	if(xfreq_P == NULL || xTime_P ==NULL || ypts_P==NULL) {
		fprintf(stderr,"spectrum: could not allocate space\n");
		return(5);
	}
} else if(control && mode == STATIC) {
	/*                                                      
	 * allocate arrays                                      
	 */                                                     
	xfreq_P = (float* )calloc(BLOCK_SIZE,sizeof(float));           
	xTime_P = (float* )calloc(BLOCK_SIZE,sizeof(float));           
	if( bufferType == COMPLEX_BUFFER)
		ypts_P = (float* )calloc(2*BLOCK_SIZE,sizeof(float));           
	else
		ypts_P = (float* )calloc(BLOCK_SIZE,sizeof(float));           
	if(xfreq_P == NULL || xTime_P ==NULL || ypts_P==NULL) {
		fprintf(stderr,"spectrum: could not allocate space\n");
		return(5);
	}
}
count = 0;
totalCount = 0;
switch(bufferType) {
	case COMPLEX_BUFFER: 
		SET_CELL_SIZE_IN(0,sizeof(complex));
		if(numberOutputBuffers == 1)
			SET_CELL_SIZE_OUT(0,sizeof(complex));
		break;
	case FLOAT_BUFFER: 
		SET_CELL_SIZE_IN(0,sizeof(float));
		if(numberOutputBuffers == 1)
			SET_CELL_SIZE_OUT(0,sizeof(float));
		break;
	case INTEGER_BUFFER: 
		SET_CELL_SIZE_IN(0,sizeof(int));
		if(numberOutputBuffers == 1)
			SET_CELL_SIZE_OUT(0,sizeof(int));
		break;
	default: 
		fprintf(stderr,"Bad buffer type specified in spectrum \n");
		return(4);
		break;
}




Top

Main Code



 


for(samples = MIN_AVAIL(); samples > 0; --samples) {


  for(i=0; i i) {
			if(IT_OUT(i)) {
				KrnOverflow("spectrum",i);
				return(99);
			}

			switch(bufferType) {
				case COMPLEX_BUFFER:
	 				OUTCX(i,0) = INCX(i,0);
					break;
				case INTEGER_BUFFER:
	 				OUTI(i,0) = INI(i,0);
					break;
				case FLOAT_BUFFER:
	 				OUTF(i,0) = INF(i,0);
					break;
			}

		}
  }
  if(++totalCount > skip && control ) {
		if(mode == STATIC)
			count=blockOff + bufi;

		bufi++;

		if (bufi == BLOCK_SIZE && mode==STATIC) {
			blockOff += BLOCK_SIZE;
			xfreq_P = (float *)realloc((char *) xfreq_P, 
				sizeof(float) * (blockOff + BLOCK_SIZE));
			xTime_P = (float *)realloc((char *) xTime_P, 
				sizeof(float) * (blockOff + BLOCK_SIZE));
			if(bufferType == COMPLEX_BUFFER) 
			    ypts_P = (float *)realloc((char *) ypts_P, 
				sizeof(float) * 2*(blockOff + BLOCK_SIZE));
			else
			    ypts_P = (float *)realloc((char *) ypts_P, 
				sizeof(float) * (blockOff + BLOCK_SIZE));
				
			bufi=0;
			if(xfreq_P == NULL || ypts_P==NULL || xTime_P==NULL) {
				fprintf(stderr,"spectrum: could not allocate space\n");
				return(6);
			}
		}

		switch(bufferType) {
			case COMPLEX_BUFFER:
				val=INCX(0,0);
          	     		ypts_P[2*count] = val.re; 
          	     		ypts_P[2*count+1] = val.im; 
				break;
			case INTEGER_BUFFER:
          	     		ypts_P[count] = INI(0,0);
				break;
			case FLOAT_BUFFER:
          	     		ypts_P[count] = INF(0,0);
				break;
		}

		if(mode == DYNAMIC)
			count++;
		
  }

} 

return(0);





Top

Wrapup Code



 

if(control == 0) return(0);
if((totalCount - skip) > 0 && mode ==STATIC) {
	/*
	 * compute the power of 2 number of fft points
	 */
	fftexp = (int) (log((float)count)/log(2.0)+0.5);
	fftl = 1 << fftexp;
	if (fftl > count ) {
		    fftl = fftl/2;
		    fftexp -= 1;
	}
	spect_P = (float* )calloc(2*fftl,sizeof(float));           
	xfreq_P = (float *) realloc((char *) xfreq_P, sizeof(float) *
				 (blockOff + bufi));
	xTime_P = (float *) realloc((char *) xTime_P, sizeof(float) *
				 (blockOff + bufi));
	if(bufferType == COMPLEX_BUFFER) 
		ypts_P = (float *) realloc((char *) ypts_P, sizeof(float) *
				 2*(blockOff + bufi));
	else
		ypts_P = (float *) realloc((char *) ypts_P, sizeof(float) *
				 (blockOff + bufi));
	if(xfreq_P == NULL || ypts_P==NULL || xTime_P == NULL) {
		fprintf(stderr,"spectrum: could not allocate space\n");
		return(8);
	}
for(i=0; i 0 ? 1:-1);
                                else
                                        phase_P[i]= atan2((double)spect_P[2*i+1],(double)spect_P[2*i]);
	    } else {
		spect_P[i]=sqrt(spect_P[2*i]*spect_P[2*i] +
				spect_P[2*i+1]*spect_P[2*i+1]);		
	        if(dbFlag) {
			tmp = spect_P[i];
			if(tmp)
			spect_P[i] = 10*log10(tmp*tmp);
			else spect_P[i] = -200.0;
		 }
	    }
	     if(sampFreq>0) 
		xfreq_P[i] = i*sampFreq/fftl;
	     else if(sampFreq == 0)
		xfreq_P[i] = i;
	     else
		xfreq_P[i] = (float)i/(float)fftl;
	     if(sampFreq>0) 
		xTime_P[i] = i/sampFreq;
	     else if(sampFreq == 0)
		xTime_P[i] = (float)i;
	     else
		xTime_P[i] = (float)i;
}
{
	/*
	 * text mode, write results to file
	 */
#if 1
	strcpy(fname,title);
	strcat(fname,".tim");
	time_F = fopen(fname,"w");
        fprintf(stderr,"spectrum created file: %s \n",fname);
	strcpy(fname,title);
	strcat(fname,".fre");
	if(bufferType == COMPLEX_BUFFER) {
		fprintf(time_F,"#complex time domain \n");
		for(i=0; i
    
Top

License



/*  Capsim (r) Text Mode Kernel (TMK) Star Library (Blocks)
    Copyright (C) 1989-2017  Silicon DSP Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    http://www.silicondsp.com
    Silicon DSP  Corporation
    Las Vegas, Nevada
*/


Top

Description



 

/* spectrumtxt.s */
/**********************************************************************
                                spectrumtxt()
***********************************************************************
        inputs:         (One channel)
        outputs:        (optional feed-through of input channels)
        parameters:     int npts, the number of points to plot
                        int skip, number of points to skip before first plot
                        file title,  the title of the plot
                        dB flag (0=linear 1=dB)
                        window flag (0=Rect 1= Hamming Hanning=2 Blackman=3)
*************************************************************************
This routine will produces the time domain and frequency domain spectrum 
of the input buffer.
The first parameter represents the number of points plotted from the channel.
Default is set to (int 128).
The second parameter is the number of points to skip before the first plot set
is presented.  This enables skipping of any transient period.
Default is set to (int 0).
The third parameter represents the title for the plot.
Default is set to "PLOT".
The fourth parameter is the dB flag (0 for linear, 1 for dB );
The fifth parameter is the window flag ( 0 for Rectangular 1 for hammming)
The sixth paramter determines the plotting style.
The seventh parameter is used to turn the time domain plot on and off.
The last parameter is used to plot agains bin number , frequency, or
normalized axis.
Programmer: 	Sasan Ardalan, Ramin Nobakht
Date: 		2/16/89
Modified:	L.J. Faber 1/3/89.  Add flow through; general cleanup.
modified for Hanning & Blackman window       Ali Sadri, Oct. 11, 1991.
Modified:	Sasan Ardalan, June 3, 1993 Added Dynamic Display.
*/