Explorar o código

clear compiler warnings

richarddobson %!s(int64=3) %!d(string=hai) anos
pai
achega
d6b8528ddc
Modificáronse 6 ficheiros con 2077 adicións e 1959 borrados
  1. 786 763
      dev/science/chirikov.c
  2. 27 3
      dev/science/multisynth.c
  3. 427 403
      dev/science/notchinvert.c
  4. 490 467
      dev/science/peakiso.c
  5. 1 1
      dev/science/pulser.c
  6. 346 322
      dev/science/smooth.c

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 786 - 763
dev/science/chirikov.c


+ 27 - 3
dev/science/multisynth.c

@@ -1,3 +1,27 @@
+/*
+ * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
+ * http://www.trevorwishart.co.uk
+ * http://www.composersdesktop.com
+ *
+ This file is part of the CDP System.
+ 
+ The CDP System 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.
+ 
+ The CDP System 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 the CDP System; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA
+ *
+ */
+
 //  COMMENT
 //  Works now for instruments with spectra defined internally
 //  With note-loudness-envelope
@@ -224,7 +248,7 @@ static double cellenv[12]  =   {0.0, 0,
 
 char errstr[2400];
 
-static int testing = 0;
+//static int testing = 0;
 
 int anal_infiles = 1;
 int sloom = 0;
@@ -248,7 +272,7 @@ static int mark_parameter_types(dataptr dz,aplptr ap);
 static int assign_file_data_storage(int infilecnt,dataptr dz);
 static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
 static int get_the_process_no(char *prog_identifier_from_cmdline,dataptr dz);
-static int get_the_mode_from_cmdline(char *str,dataptr dz);
+//static int get_the_mode_from_cmdline(char *str,dataptr dz);
 static int setup_and_init_input_brktable_constants(dataptr dz,int brkcnt);
 
 static int synthesis_param_preprocess(dataptr dz);
@@ -259,7 +283,7 @@ static int create_synthesizer_sndbufs(dataptr dz);
 static int generate_packet_envelope (dataptr dz);
 static double read_packet_envelope(int kk,double incr,dataptr dz);
 static int modify_packet_envelope(double synth_ctr,double synth_sqz,dataptr dz);
-static double get_frq_with_vibrato(int *init,int n,double onehzincr,double srate,dataptr dz);
+//static double get_frq_with_vibrato(int *init,int n,double onehzincr,double srate,dataptr dz);
 
 static int pretest_the_special_data(char *cmdline,int *inlinecnt,synptr *orchestra,dataptr dz);
 static int setup_the_special_data_for_given_instr(int instrno,double *data,synptr instrument,dataptr dz);

+ 427 - 403
dev/science/notchinvert.c

@@ -1,10 +1,34 @@
+/*
+ * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
+ * http://www.trevorwishart.co.uk
+ * http://www.composersdesktop.com
+ *
+ This file is part of the CDP System.
+ 
+ The CDP System 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.
+ 
+ The CDP System 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 the CDP System; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA
+ *
+ */
+
 /*
  * Take a set of amp-frq data in the correct range,
  * find the notches in the spectrum.
  * Create a new spectrum by inverting these notches to become peaks
  */
 
-#define PSEUDOCENT	(1.0009)	// approx 1/64th of a tone
+#define PSEUDOCENT  (1.0009)    // approx 1/64th of a tone
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -26,13 +50,13 @@
 #include <string.h>
 #include <srates.h>
 
-int	sloom = 0;
+int sloom = 0;
 int sloombatch = 0;
 
 const char* cdp_version = "6.1.0";
 
 //CDP LIB REPLACEMENTS
-static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
+//static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
 static int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt);
 static int test_the_special_data(double *parray, int itemcnt, double nyquist);
 static int invnotch(double *parray,int itemcnt,double *outarray,double minnotch);
@@ -42,67 +66,67 @@ static int get_float_with_e_from_within_string(char **str,double *val);
 
 int main(int argc,char *argv[])
 {
-	FILE *fpi, *fpo;
-	int k;
-	int exit_status;
-	double *parray, *outarray;
-	int itemcnt, srate;
-	double minnotch = 0.0, nyquist;
-	char temp[200], temp2[200];
-	if(argc < 4 || argc > 5) {
-		usage1();	
-		return(FAILED);
-	}
-	if(argc == 5) {
-		if(sscanf(argv[4],"%lf",&minnotch) != 1) {
-			fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
-			return(FAILED);
-		}
-		if(minnotch > 1.0 || minnotch < 0.0) {
-			fprintf(stderr,"INVALID MINIMUM NOTCH %s (range 0 - 1)\n",argv[3]);
-			return(FAILED);
-		}
-	} 
-	if(sscanf(argv[3],"%d",&srate) != 1) {
-		fprintf(stderr,"CANNOT READ SAMPLERATE (%s)\n",argv[3]);
-		return(FAILED);
-	}
- 	if(srate < 44100 || BAD_SR(srate)) {
-		fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
-		return(FAILED);
-	}
-	nyquist = (double)(srate/2);
-	if((fpi = fopen(argv[1],"r")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
-		return(FAILED);
-	}
-	if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
-		return(FAILED);
-	if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
-		return(FAILED);
-	if((outarray = (double *)malloc(itemcnt * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	if((fpo = fopen(argv[2],"w")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
-		return(FAILED);
-	}
-	if((exit_status = invnotch(parray,itemcnt,outarray,minnotch)) < 0)
-		return(FAILED);
-	k = 0;
-	for(k = 0;k < itemcnt;k+=2) {
-		sprintf(temp,"%lf",outarray[k]);
-		sprintf(temp2,"%lf",outarray[k+1]);
-		strcat(temp2,"\n");
-		strcat(temp,"\t");
-		strcat(temp,temp2);
-		fputs(temp,fpo);
-	}
-	if(fclose(fpo)<0) {
-		fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
-	}
-	return(SUCCEEDED);
+    FILE *fpi, *fpo;
+    int k;
+    int exit_status;
+    double *parray, *outarray;
+    int itemcnt, srate;
+    double minnotch = 0.0, nyquist;
+    char temp[200], temp2[200];
+    if(argc < 4 || argc > 5) {
+        usage1();   
+        return(FAILED);
+    }
+    if(argc == 5) {
+        if(sscanf(argv[4],"%lf",&minnotch) != 1) {
+            fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
+            return(FAILED);
+        }
+        if(minnotch > 1.0 || minnotch < 0.0) {
+            fprintf(stderr,"INVALID MINIMUM NOTCH %s (range 0 - 1)\n",argv[3]);
+            return(FAILED);
+        }
+    } 
+    if(sscanf(argv[3],"%d",&srate) != 1) {
+        fprintf(stderr,"CANNOT READ SAMPLERATE (%s)\n",argv[3]);
+        return(FAILED);
+    }
+    if(srate < 44100 || BAD_SR(srate)) {
+        fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
+        return(FAILED);
+    }
+    nyquist = (double)(srate/2);
+    if((fpi = fopen(argv[1],"r")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
+        return(FAILED);
+    }
+    if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
+        return(FAILED);
+    if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
+        return(FAILED);
+    if((outarray = (double *)malloc(itemcnt * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    if((fpo = fopen(argv[2],"w")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
+        return(FAILED);
+    }
+    if((exit_status = invnotch(parray,itemcnt,outarray,minnotch)) < 0)
+        return(FAILED);
+    k = 0;
+    for(k = 0;k < itemcnt;k+=2) {
+        sprintf(temp,"%lf",outarray[k]);
+        sprintf(temp2,"%lf",outarray[k+1]);
+        strcat(temp2,"\n");
+        strcat(temp,"\t");
+        strcat(temp,temp2);
+        fputs(temp,fpo);
+    }
+    if(fclose(fpo)<0) {
+        fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
+    }
+    return(SUCCEEDED);
 }
 
 
@@ -110,333 +134,333 @@ int main(int argc,char *argv[])
 
 int usage1(void)
 {
-	fprintf(stderr,
-	"USAGE: notchinvert datafile outdatafile srate [minnotch]\n"
-	"\n"
-	"Invert notches in a spectrum.\n"
-	"\n"
-	"DATAFILE     textfile of frq/amp pairs.\n"
-	"OUTDATAFILE  inverted notches as frq/amp pairs.\n"
-	"MINNOTCH     min depth of notch to qualify as true notch (Range 0-1).\n"
-	"\n");
-	return(USAGE_ONLY);
+    fprintf(stderr,
+    "USAGE: notchinvert datafile outdatafile srate [minnotch]\n"
+    "\n"
+    "Invert notches in a spectrum.\n"
+    "\n"
+    "DATAFILE     textfile of frq/amp pairs.\n"
+    "OUTDATAFILE  inverted notches as frq/amp pairs.\n"
+    "MINNOTCH     min depth of notch to qualify as true notch (Range 0-1).\n"
+    "\n");
+    return(USAGE_ONLY);
 }
 
 /************************** HANDLE_THE_SPECIAL_DATA **********************************/
 
 int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt)
 {
-	int cnt;
-	double *p, dummy;
-	char temp[200], *q;
-	cnt = 0;
-	p = &dummy;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p))
-			cnt++;
-	}
-	if(ODD(cnt)) {
-		fprintf(stderr,"Data not paired correctly in input data file\n");
-		return(DATA_ERROR);
-	}
-	if(cnt == 0) {
-		fprintf(stderr,"No data in input data file\n");
-		return(DATA_ERROR);
-	}
-	if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	fseek(fpi,0,0);
-	p = *parray;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p)) {
-			p++;
-		}
-	}
-	if(fclose(fpi)<0) {
-		fprintf(stderr,"WARNING: Failed to close input data file.\n");
-	}
-	*itemcnt = cnt;
-	return(FINISHED);
+    int cnt;
+    double *p, dummy;
+    char temp[200], *q;
+    cnt = 0;
+    p = &dummy;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p))
+            cnt++;
+    }
+    if(ODD(cnt)) {
+        fprintf(stderr,"Data not paired correctly in input data file\n");
+        return(DATA_ERROR);
+    }
+    if(cnt == 0) {
+        fprintf(stderr,"No data in input data file\n");
+        return(DATA_ERROR);
+    }
+    if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    fseek(fpi,0,0);
+    p = *parray;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p)) {
+            p++;
+        }
+    }
+    if(fclose(fpi)<0) {
+        fprintf(stderr,"WARNING: Failed to close input data file.\n");
+    }
+    *itemcnt = cnt;
+    return(FINISHED);
 }
 
 /************************** TEST_THE_SPECIAL_DATA **********************************/
 
 int test_the_special_data(double *parray, int itemcnt, double nyquist)
 {
-	double *p;
-	int isamp = 0, linecnt = 1;
-	p = parray;
-	while(linecnt <= itemcnt/2) {
-		if(isamp) {
-			if(*p < 0.0 || *p > 1.0) {
-				fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
-				return(DATA_ERROR);
-			}
-		} else {
-			if(*p < 0.0 || *p > nyquist) {
-				fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
-				return(DATA_ERROR);
-			}
-		}
-		if(!isamp)
-			linecnt++;
-		isamp = !isamp;
-		p++;
-	}
-	return(FINISHED);
+    double *p;
+    int isamp = 0, linecnt = 1;
+    p = parray;
+    while(linecnt <= itemcnt/2) {
+        if(isamp) {
+            if(*p < 0.0 || *p > 1.0) {
+                fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
+                return(DATA_ERROR);
+            }
+        } else {
+            if(*p < 0.0 || *p > nyquist) {
+                fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
+                return(DATA_ERROR);
+            }
+        }
+        if(!isamp)
+            linecnt++;
+        isamp = !isamp;
+        p++;
+    }
+    return(FINISHED);
 }
 
 /************************** INVNOTCH **********************************/
 
 int invnotch(double *parray,int itemcnt,double *outarray,double minnotch)
 {
-	int ampat, trofampat, pkampbelowat, pkampaboveat, nextpkat, writeat, j;
-	double amp, lastamp, ampstep, lastampstep, belowstep, abovestep, notchdepth, subtractor;
-	double *pkbefore, *pkafter, *notch;
-	int trofcnt, badnotches, jj, kk;
-	int *pkbeforeat, *notchat;
+    int ampat, trofampat, pkampbelowat, pkampaboveat, nextpkat, writeat, j;
+    double amp, lastamp, ampstep, lastampstep, belowstep, abovestep, notchdepth, subtractor;
+    double *pkbefore, *pkafter, *notch;
+    int trofcnt, badnotches, jj, kk;
+    int *pkbeforeat, *notchat;
 
-	memset((char *)outarray,0,itemcnt * sizeof(double));
+    memset((char *)outarray,0,itemcnt * sizeof(double));
 
-		/* FIND PEAKS AND TROUGHS */
+        /* FIND PEAKS AND TROUGHS */
 
-	lastamp = parray[1];
-	lastampstep = 0;
-	ampat = 3;
-	trofcnt = 0;
-	while(ampat < itemcnt) {
-		amp = parray[ampat];
-		ampstep = amp - lastamp;
-		if(ampstep < 0) {		
-			if(lastampstep > 0) {			// falling after rise = peak
-				outarray[ampat-2] = 1.0;	//	peak
-			}
-			lastampstep = ampstep;
-		} else if(ampstep > 0) {
-			if(lastampstep < 0) {			// rising after fall = trough
-				outarray[ampat-2] = -1.0;	//	trough
-				trofcnt++;
-			}
-			lastampstep = ampstep;
-		}									//	else, if flat : lastampstep not updated
-		lastamp = amp;						//	saddle points are thus ignored, while flat peaks or troughs are spotted
-		ampat += 2;
-	}
-	if(trofcnt == 0) {
-		fprintf(stderr,"NO NOTCHES FOUND\n");
-		for(j = 0; j < itemcnt; j+=2) {
-			outarray[j] = parray[j];
-			outarray[j+1] = 0.0;
-		}
-		return FINISHED;
-	}
-	if((pkbefore = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
-		return(MEMORY_ERROR);
-	}
-	if((pkafter = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	if((pkbeforeat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
-		return(MEMORY_ERROR);
-	}
-	if((notch = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	if((notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	trofcnt = 0;
-	ampat = 1;
-	pkampbelowat = -1;						//	position of peak before trough
-	pkampaboveat = -1;						//	position of peak after trough
-	trofampat = -1;							//	position of trough
+    lastamp = parray[1];
+    lastampstep = 0;
+    ampat = 3;
+    trofcnt = 0;
+    while(ampat < itemcnt) {
+        amp = parray[ampat];
+        ampstep = amp - lastamp;
+        if(ampstep < 0) {       
+            if(lastampstep > 0) {           // falling after rise = peak
+                outarray[ampat-2] = 1.0;    //  peak
+            }
+            lastampstep = ampstep;
+        } else if(ampstep > 0) {
+            if(lastampstep < 0) {           // rising after fall = trough
+                outarray[ampat-2] = -1.0;   //  trough
+                trofcnt++;
+            }
+            lastampstep = ampstep;
+        }                                   //  else, if flat : lastampstep not updated
+        lastamp = amp;                      //  saddle points are thus ignored, while flat peaks or troughs are spotted
+        ampat += 2;
+    }
+    if(trofcnt == 0) {
+        fprintf(stderr,"NO NOTCHES FOUND\n");
+        for(j = 0; j < itemcnt; j+=2) {
+            outarray[j] = parray[j];
+            outarray[j+1] = 0.0;
+        }
+        return FINISHED;
+    }
+    if((pkbefore = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
+        return(MEMORY_ERROR);
+    }
+    if((pkafter = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    if((pkbeforeat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
+        return(MEMORY_ERROR);
+    }
+    if((notch = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    if((notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    trofcnt = 0;
+    ampat = 1;
+    pkampbelowat = -1;                      //  position of peak before trough
+    pkampaboveat = -1;                      //  position of peak after trough
+    trofampat = -1;                         //  position of trough
 
-	/* NOTE LEVELS BEFORE AND AFTER NOTCHES AND MARK BAD NOTCHES */
+    /* NOTE LEVELS BEFORE AND AFTER NOTCHES AND MARK BAD NOTCHES */
 
-	badnotches = 0;
+    badnotches = 0;
 
-	while(ampat < itemcnt) {
-		amp = outarray[ampat];
-		if(amp < 0.0) {
-			if(pkampbelowat < 0) {
-				pkampbelowat = 1;
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-			}
-			trofampat = ampat;
-			notch[trofcnt] = parray[ampat];
-			notchat[trofcnt] = ampat;
-		} else if(amp > 0.0) {				//	found peak
-			if(trofampat < 0) {				//	if not yet found trof, this is the peak below the trof
-				pkampbelowat = ampat;
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-			} else {						//	otherwise, its peak above trof : we now have complete notch
-				pkampaboveat = ampat;
-				pkafter[trofcnt] = parray[pkampaboveat];
-				belowstep = pkbefore[trofcnt] - parray[trofampat];
-				abovestep = pkafter[trofcnt]  - parray[trofampat];
-				notchdepth = min(belowstep,abovestep);
-				if(notchdepth < minnotch)
-					badnotches = 1;
-				trofcnt++;
-				pkampbelowat = pkampaboveat; //	move to next notch
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-				pkampaboveat = -1;
-				trofampat = -1;
-			}
-		}
-		ampat += 2;							//	if no peak found after last trough .. 
-	}										//	and last trof is not at very end (shoulf be impossible)
-	if(trofampat >= 0 && (trofampat != itemcnt-1) && (pkampaboveat < 0)) {
-		pkampaboveat = itemcnt - 1;			//	set peak to top edge of spectrum
-		pkafter[trofcnt]  = parray[pkampaboveat];
-		belowstep = pkbefore[trofcnt] - parray[trofampat];
-		abovestep = pkafter[trofcnt]  - parray[trofampat];
-		notchdepth = min(belowstep,abovestep);
-		if(notchdepth < minnotch)			//	Mark notches that are not deep enough
-			badnotches = 1;
-		trofcnt++;
-		pkbefore[trofcnt] = parray[pkampaboveat];	//	Extra "pkbefore" after last notch
-		pkbeforeat[trofcnt] = pkampaboveat;
-	}
+    while(ampat < itemcnt) {
+        amp = outarray[ampat];
+        if(amp < 0.0) {
+            if(pkampbelowat < 0) {
+                pkampbelowat = 1;
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+            }
+            trofampat = ampat;
+            notch[trofcnt] = parray[ampat];
+            notchat[trofcnt] = ampat;
+        } else if(amp > 0.0) {              //  found peak
+            if(trofampat < 0) {             //  if not yet found trof, this is the peak below the trof
+                pkampbelowat = ampat;
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+            } else {                        //  otherwise, its peak above trof : we now have complete notch
+                pkampaboveat = ampat;
+                pkafter[trofcnt] = parray[pkampaboveat];
+                belowstep = pkbefore[trofcnt] - parray[trofampat];
+                abovestep = pkafter[trofcnt]  - parray[trofampat];
+                notchdepth = min(belowstep,abovestep);
+                if(notchdepth < minnotch)
+                    badnotches = 1;
+                trofcnt++;
+                pkampbelowat = pkampaboveat; // move to next notch
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+                pkampaboveat = -1;
+                trofampat = -1;
+            }
+        }
+        ampat += 2;                         //  if no peak found after last trough .. 
+    }                                       //  and last trof is not at very end (shoulf be impossible)
+    if(trofampat >= 0 && (trofampat != itemcnt-1) && (pkampaboveat < 0)) {
+        pkampaboveat = itemcnt - 1;         //  set peak to top edge of spectrum
+        pkafter[trofcnt]  = parray[pkampaboveat];
+        belowstep = pkbefore[trofcnt] - parray[trofampat];
+        abovestep = pkafter[trofcnt]  - parray[trofampat];
+        notchdepth = min(belowstep,abovestep);
+        if(notchdepth < minnotch)           //  Mark notches that are not deep enough
+            badnotches = 1;
+        trofcnt++;
+        pkbefore[trofcnt] = parray[pkampaboveat];   //  Extra "pkbefore" after last notch
+        pkbeforeat[trofcnt] = pkampaboveat;
+    }
 
-	/* ELIMINATE ADJACENT BAD NOTCHES */
-		
-	if(badnotches) {
+    /* ELIMINATE ADJACENT BAD NOTCHES */
+        
+    if(badnotches) {
 
-		for(kk = 0;kk < trofcnt;kk++) {									//	x	      x
-			if(pkbefore[kk] - notch[kk]	< minnotch) {					//	 x   o   x
-				if((kk>0) && (pkbefore[kk] - notch[kk-1] < minnotch)) {	//    x x x x
-					//ELIMINATE PEAK									//	   x   o
-					outarray[pkbeforeat[kk]] = 0.0;
-					// KEEP DEEPEST NOTCH
-					if(notch[kk] <= notch[kk-1]) {					//		x	      x
-						outarray[notchat[kk-1]] = 0.0;				//		 x   o   x
-						notch[kk-1] = notch[kk];					//		  x x x x
-						notchat[kk-1]= notchat[kk];					//		   +   o			
-					} else {
-						outarray[notchat[kk]] = 0.0;
-					}
-					for(jj = kk+1;jj < trofcnt; jj++) {
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-						notch[jj-1] = notch[jj];
-						notchat[jj-1] = notchat[jj];
-					}
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-				} else if(pkbefore[kk+1] - notch[kk] < minnotch) {	//		   o   +
-					//ELIMINATE NOTCH								//		  x x x x
- 					outarray[notchat[kk]] = 0.0;					//		 x   o	 x
-					//ELIMINATE LOWEST PEAK							//		x		  x
-					if(pkbefore[kk] >= pkbefore[kk+1]) {
-						outarray[pkbeforeat[kk+1]] = 0.0;
-						if(kk < trofcnt-1) {
-							notch[kk] = notch[kk+1];
-							notchat[kk] = notchat[kk+1];
-						}
-						for(jj = kk+2;jj < trofcnt; jj++) {
-							pkbefore[jj-1] = pkbefore[jj];
-							pkbeforeat[jj-1] = pkbeforeat[jj];
-							notch[jj-1] = notch[jj];
-							notchat[jj-1] = notchat[jj];
-						}
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-					} else {
-						outarray[pkbeforeat[kk]] = 0.0;
-						for(jj = kk+1;jj < trofcnt; jj++) {
-							pkbefore[jj-1] = pkbefore[jj];
-							pkbeforeat[jj-1] = pkbeforeat[jj];
-							notch[jj-1] = notch[jj];
-							notchat[jj-1] = notchat[jj];
-						}
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-					}
-				} else {
-					//ELIMINATE PEAK BEFORE	AND NOTCH				//			x
-					outarray[pkbeforeat[kk]] = 0.0;					//	   o   x
-					outarray[notchat[kk]] = 0.0;					//	  x x x
-					for(jj = kk+1;jj < trofcnt; jj++) {				//	 x   o
-						pkbefore[jj-1] = pkbefore[jj];				//  x
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-						notch[jj-1] = notch[jj];
-						notchat[jj-1] = notchat[jj];
-					}
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-				}
-				kk--;
-				trofcnt--;
-			} else if(pkbefore[kk+1] - notch[kk] < minnotch) {		//  x
-				//ELIMINATE PEAK AFTER AND NOTCH					//	 x   o
-				outarray[pkbeforeat[kk+1]] = 0.0;					//	  x x x
-				outarray[notchat[kk]] = 0.0;						//	   o   x
-				if(kk < trofcnt-1) {								//		    x
-					notch[kk] = notch[kk+1];
-					notchat[kk] = notchat[kk+1];
-				}
-				for(jj = kk+2;jj < trofcnt; jj++) {
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-					notch[jj-1] = notch[jj];
-					notchat[jj-1] = notchat[jj];
-				}
-				pkbefore[jj-1] = pkbefore[jj];
-				pkbeforeat[jj-1] = pkbeforeat[jj];
-				kk--;
-				trofcnt--;
-			}
-		}
-	}
-	writeat = 0;							//	position of write to output
-	kk = 0;
-	while(writeat < pkbeforeat[0]-1) {		//	zero output before 1st peak
-		outarray[writeat] = parray[writeat];
-		writeat++;
-		outarray[writeat] = 0.0;
-		writeat++;
-	}
-	for(kk = 0;kk < trofcnt;kk++) {
-		nextpkat = pkbeforeat[kk+1] - 1;	//	nextpkat points to the PAIR of values: pkbeforeat points to its amplitude
-		belowstep = pkbefore[kk]   - notch[kk];
-		abovestep = pkbefore[kk+1] - notch[kk];
-		notchdepth = min(belowstep,abovestep);
-		if(belowstep < abovestep)
-			subtractor = pkbefore[kk];
-		else
-			subtractor = pkbefore[kk+1];
-		while(writeat < nextpkat) {
-			outarray[writeat] = parray[writeat];
-			writeat++;
-			outarray[writeat] = min(parray[writeat] - subtractor,0.0);
-			outarray[writeat] = -outarray[writeat];
-			writeat++;
-		}
-	}
-	while(writeat < itemcnt) {				//	zero output after last peak
-		outarray[writeat] = parray[writeat];
-		writeat++;
-		outarray[writeat] = 0.0;
-		writeat++;
-	}
-	return(FINISHED);
+        for(kk = 0;kk < trofcnt;kk++) {                                 //  x         x
+            if(pkbefore[kk] - notch[kk] < minnotch) {                   //   x   o   x
+                if((kk>0) && (pkbefore[kk] - notch[kk-1] < minnotch)) { //    x x x x
+                    //ELIMINATE PEAK                                    //     x   o
+                    outarray[pkbeforeat[kk]] = 0.0;
+                    // KEEP DEEPEST NOTCH
+                    if(notch[kk] <= notch[kk-1]) {                  //      x         x
+                        outarray[notchat[kk-1]] = 0.0;              //       x   o   x
+                        notch[kk-1] = notch[kk];                    //        x x x x
+                        notchat[kk-1]= notchat[kk];                 //         +   o            
+                    } else {
+                        outarray[notchat[kk]] = 0.0;
+                    }
+                    for(jj = kk+1;jj < trofcnt; jj++) {
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                        notch[jj-1] = notch[jj];
+                        notchat[jj-1] = notchat[jj];
+                    }
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                } else if(pkbefore[kk+1] - notch[kk] < minnotch) {  //         o   +
+                    //ELIMINATE NOTCH                               //        x x x x
+                    outarray[notchat[kk]] = 0.0;                    //       x   o   x
+                    //ELIMINATE LOWEST PEAK                         //      x         x
+                    if(pkbefore[kk] >= pkbefore[kk+1]) {
+                        outarray[pkbeforeat[kk+1]] = 0.0;
+                        if(kk < trofcnt-1) {
+                            notch[kk] = notch[kk+1];
+                            notchat[kk] = notchat[kk+1];
+                        }
+                        for(jj = kk+2;jj < trofcnt; jj++) {
+                            pkbefore[jj-1] = pkbefore[jj];
+                            pkbeforeat[jj-1] = pkbeforeat[jj];
+                            notch[jj-1] = notch[jj];
+                            notchat[jj-1] = notchat[jj];
+                        }
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                    } else {
+                        outarray[pkbeforeat[kk]] = 0.0;
+                        for(jj = kk+1;jj < trofcnt; jj++) {
+                            pkbefore[jj-1] = pkbefore[jj];
+                            pkbeforeat[jj-1] = pkbeforeat[jj];
+                            notch[jj-1] = notch[jj];
+                            notchat[jj-1] = notchat[jj];
+                        }
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                    }
+                } else {
+                    //ELIMINATE PEAK BEFORE AND NOTCH               //          x
+                    outarray[pkbeforeat[kk]] = 0.0;                 //     o   x
+                    outarray[notchat[kk]] = 0.0;                    //    x x x
+                    for(jj = kk+1;jj < trofcnt; jj++) {             //   x   o
+                        pkbefore[jj-1] = pkbefore[jj];              //  x
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                        notch[jj-1] = notch[jj];
+                        notchat[jj-1] = notchat[jj];
+                    }
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                }
+                kk--;
+                trofcnt--;
+            } else if(pkbefore[kk+1] - notch[kk] < minnotch) {      //  x
+                //ELIMINATE PEAK AFTER AND NOTCH                    //   x   o
+                outarray[pkbeforeat[kk+1]] = 0.0;                   //    x x x
+                outarray[notchat[kk]] = 0.0;                        //     o   x
+                if(kk < trofcnt-1) {                                //          x
+                    notch[kk] = notch[kk+1];
+                    notchat[kk] = notchat[kk+1];
+                }
+                for(jj = kk+2;jj < trofcnt; jj++) {
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                    notch[jj-1] = notch[jj];
+                    notchat[jj-1] = notchat[jj];
+                }
+                pkbefore[jj-1] = pkbefore[jj];
+                pkbeforeat[jj-1] = pkbeforeat[jj];
+                kk--;
+                trofcnt--;
+            }
+        }
+    }
+    writeat = 0;                            //  position of write to output
+    kk = 0;
+    while(writeat < pkbeforeat[0]-1) {      //  zero output before 1st peak
+        outarray[writeat] = parray[writeat];
+        writeat++;
+        outarray[writeat] = 0.0;
+        writeat++;
+    }
+    for(kk = 0;kk < trofcnt;kk++) {
+        nextpkat = pkbeforeat[kk+1] - 1;    //  nextpkat points to the PAIR of values: pkbeforeat points to its amplitude
+        belowstep = pkbefore[kk]   - notch[kk];
+        abovestep = pkbefore[kk+1] - notch[kk];
+        notchdepth = min(belowstep,abovestep);
+        if(belowstep < abovestep)
+            subtractor = pkbefore[kk];
+        else
+            subtractor = pkbefore[kk+1];
+        while(writeat < nextpkat) {
+            outarray[writeat] = parray[writeat];
+            writeat++;
+            outarray[writeat] = min(parray[writeat] - subtractor,0.0);
+            outarray[writeat] = -outarray[writeat];
+            writeat++;
+        }
+    }
+    while(writeat < itemcnt) {              //  zero output after last peak
+        outarray[writeat] = parray[writeat];
+        writeat++;
+        outarray[writeat] = 0.0;
+        writeat++;
+    }
+    return(FINISHED);
 }
 
 /************************** GET_FLOAT_WITH_E_FROM_WITHIN_STRING **************************
@@ -447,43 +471,43 @@ int invnotch(double *parray,int itemcnt,double *outarray,double minnotch)
 
 int  get_float_with_e_from_within_string(char **str,double *val)
 {
-	char   *p, *valstart;
-	int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
-	p = *str;
-	while(isspace(*p))
-		p++;
-	valstart = p;	
-	switch(*p) {
-	case('-'):						break;
-	case('.'): decimal_point_cnt=1;	break;
-	default:
-		if(!isdigit(*p))
-			return(FALSE);
-		has_digits = TRUE;
-		break;
-	}
-	p++;
-	while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
-		if(isdigit(*p))
-			has_digits = TRUE;
-		else if(*p == 'e') {
-			if(has_e || !has_digits)
-				return(FALSE);
-			has_e = 1;
-		} else if(*p == '-') {
-			if(!has_e || (lastchar != 'e'))
-				return(FALSE);
-		} else if(*p == '.') {
-			if(has_e || (++decimal_point_cnt>1))
-				return(FALSE);
-		} else
-			return(FALSE);
-		lastchar = *p;
-		p++;
-	}
-	if(!has_digits || sscanf(valstart,"%lf",val)!=1)
-		return(FALSE);
-	*str = p;
-	return(TRUE);
+    char   *p, *valstart;
+    int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
+    p = *str;
+    while(isspace(*p))
+        p++;
+    valstart = p;   
+    switch(*p) {
+    case('-'):                      break;
+    case('.'): decimal_point_cnt=1; break;
+    default:
+        if(!isdigit(*p))
+            return(FALSE);
+        has_digits = TRUE;
+        break;
+    }
+    p++;
+    while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
+        if(isdigit(*p))
+            has_digits = TRUE;
+        else if(*p == 'e') {
+            if(has_e || !has_digits)
+                return(FALSE);
+            has_e = 1;
+        } else if(*p == '-') {
+            if(!has_e || (lastchar != 'e'))
+                return(FALSE);
+        } else if(*p == '.') {
+            if(has_e || (++decimal_point_cnt>1))
+                return(FALSE);
+        } else
+            return(FALSE);
+        lastchar = *p;
+        p++;
+    }
+    if(!has_digits || sscanf(valstart,"%lf",val)!=1)
+        return(FALSE);
+    *str = p;
+    return(TRUE);
 }
 

+ 490 - 467
dev/science/peakiso.c

@@ -1,10 +1,33 @@
+/*
+ * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
+ * http://www.trevorwishart.co.uk
+ * http://www.composersdesktop.com
+ *
+ This file is part of the CDP System.
+ 
+ The CDP System 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.
+ 
+ The CDP System 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 the CDP System; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA
+ *
+ */
 /*
  * Take a set of amp-frq data in the correct range,
  * find the notches in the spectrum.
  * Create a new spectrum by inverting these notches to become peaks
  */
 
-#define PSEUDOCENT	(1.0009)	// approx 1/64th of a tone
+#define PSEUDOCENT  (1.0009)    // approx 1/64th of a tone
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -26,13 +49,13 @@
 #include <string.h>
 #include <srates.h>
 
-int	sloom = 0;
+int sloom = 0;
 int sloombatch = 0;
 
 const char* cdp_version = "6.1.0";
 
 //CDP LIB REPLACEMENTS
-static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
+//static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
 static int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt);
 static int test_the_special_data(double *parray, int itemcnt, double nyquist);
 static int peakiso(double *parray,int itemcnt,double *outarray,double minnotch);
@@ -42,67 +65,67 @@ static int get_float_with_e_from_within_string(char **str,double *val);
 
 int main(int argc,char *argv[])
 {
-	FILE *fpi, *fpo;
-	int k;
-	int exit_status;
-	double *parray, *outarray;
-	int itemcnt, srate;
-	double minnotch = 0.0, nyquist;
-	char temp[200], temp2[200];
-	if(argc < 4 || argc > 5) {
-		usage1();	
-		return(FAILED);
-	}
-	if(argc == 5) {
-		if(sscanf(argv[4],"%lf",&minnotch) != 1) {
-			fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
-			return(FAILED);
-		}
-		if(minnotch > 1.0 || minnotch < 0.0) {
-			fprintf(stderr,"INVALID MINIMUM NOTCH %s (range 0 - 1)\n",argv[3]);
-			return(FAILED);
-		}
-	} 
-	if(sscanf(argv[3],"%d",&srate) != 1) {
-		fprintf(stderr,"CANNOT READ SAMPLERATE (%s)\n",argv[3]);
-		return(FAILED);
-	}
- 	if(srate < 44100 || BAD_SR(srate)) {
-		fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
-		return(FAILED);
-	}
-	nyquist = (double)(srate/2);
-	if((fpi = fopen(argv[1],"r")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
-		return(FAILED);
-	}
-	if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
-		return(FAILED);
-	if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
-		return(FAILED);
-	if((outarray = (double *)malloc(itemcnt * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	if((fpo = fopen(argv[2],"w")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
-		return(FAILED);
-	}
-	if((exit_status = peakiso(parray,itemcnt,outarray,minnotch)) < 0)
-		return(FAILED);
-	k = 0;
-	for(k = 0;k < itemcnt;k+=2) {
-		sprintf(temp,"%lf",outarray[k]);
-		sprintf(temp2,"%lf",outarray[k+1]);
-		strcat(temp2,"\n");
-		strcat(temp,"\t");
-		strcat(temp,temp2);
-		fputs(temp,fpo);
-	}
-	if(fclose(fpo)<0) {
-		fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
-	}
-	return(SUCCEEDED);
+    FILE *fpi, *fpo;
+    int k;
+    int exit_status;
+    double *parray, *outarray;
+    int itemcnt, srate;
+    double minnotch = 0.0, nyquist;
+    char temp[200], temp2[200];
+    if(argc < 4 || argc > 5) {
+        usage1();   
+        return(FAILED);
+    }
+    if(argc == 5) {
+        if(sscanf(argv[4],"%lf",&minnotch) != 1) {
+            fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
+            return(FAILED);
+        }
+        if(minnotch > 1.0 || minnotch < 0.0) {
+            fprintf(stderr,"INVALID MINIMUM NOTCH %s (range 0 - 1)\n",argv[3]);
+            return(FAILED);
+        }
+    } 
+    if(sscanf(argv[3],"%d",&srate) != 1) {
+        fprintf(stderr,"CANNOT READ SAMPLERATE (%s)\n",argv[3]);
+        return(FAILED);
+    }
+    if(srate < 44100 || BAD_SR(srate)) {
+        fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
+        return(FAILED);
+    }
+    nyquist = (double)(srate/2);
+    if((fpi = fopen(argv[1],"r")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
+        return(FAILED);
+    }
+    if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
+        return(FAILED);
+    if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
+        return(FAILED);
+    if((outarray = (double *)malloc(itemcnt * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    if((fpo = fopen(argv[2],"w")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
+        return(FAILED);
+    }
+    if((exit_status = peakiso(parray,itemcnt,outarray,minnotch)) < 0)
+        return(FAILED);
+    k = 0;
+    for(k = 0;k < itemcnt;k+=2) {
+        sprintf(temp,"%lf",outarray[k]);
+        sprintf(temp2,"%lf",outarray[k+1]);
+        strcat(temp2,"\n");
+        strcat(temp,"\t");
+        strcat(temp,temp2);
+        fputs(temp,fpo);
+    }
+    if(fclose(fpo)<0) {
+        fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
+    }
+    return(SUCCEEDED);
 }
 
 
@@ -110,396 +133,396 @@ int main(int argc,char *argv[])
 
 int usage1(void)
 {
-	fprintf(stderr,
-	"USAGE: peakiso datafile outdatafile srate [minnotch]\n"
-	"\n"
-	"Isolate peaks in a spectrum.\n"
-	"\n"
-	"DATAFILE     textfile of frq/amp pairs.\n"
-	"OUTDATAFILE  isolated peaks as frq/amp pairs.\n"
-	"MINNOTCH     min depth of notch to qualify as peak-separator (Range 0-1).\n"
-	"\n");
-	return(USAGE_ONLY);
+    fprintf(stderr,
+    "USAGE: peakiso datafile outdatafile srate [minnotch]\n"
+    "\n"
+    "Isolate peaks in a spectrum.\n"
+    "\n"
+    "DATAFILE     textfile of frq/amp pairs.\n"
+    "OUTDATAFILE  isolated peaks as frq/amp pairs.\n"
+    "MINNOTCH     min depth of notch to qualify as peak-separator (Range 0-1).\n"
+    "\n");
+    return(USAGE_ONLY);
 }
 
 /************************** HANDLE_THE_SPECIAL_DATA **********************************/
 
 int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt)
 {
-	int cnt;
-	double *p, dummy;
-	char temp[200], *q;
-	cnt = 0;
-	p = &dummy;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p))
-			cnt++;
-	}
-	if(ODD(cnt)) {
-		fprintf(stderr,"Data not paired correctly in input data file\n");
-		return(DATA_ERROR);
-	}
-	if(cnt == 0) {
-		fprintf(stderr,"No data in input data file\n");
-		return(DATA_ERROR);
-	}
-	if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	fseek(fpi,0,0);
-	p = *parray;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p)) {
-			p++;
-		}
-	}
-	if(fclose(fpi)<0) {
-		fprintf(stderr,"WARNING: Failed to close input data file.\n");
-	}
-	*itemcnt = cnt;
-	return(FINISHED);
+    int cnt;
+    double *p, dummy;
+    char temp[200], *q;
+    cnt = 0;
+    p = &dummy;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p))
+            cnt++;
+    }
+    if(ODD(cnt)) {
+        fprintf(stderr,"Data not paired correctly in input data file\n");
+        return(DATA_ERROR);
+    }
+    if(cnt == 0) {
+        fprintf(stderr,"No data in input data file\n");
+        return(DATA_ERROR);
+    }
+    if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    fseek(fpi,0,0);
+    p = *parray;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p)) {
+            p++;
+        }
+    }
+    if(fclose(fpi)<0) {
+        fprintf(stderr,"WARNING: Failed to close input data file.\n");
+    }
+    *itemcnt = cnt;
+    return(FINISHED);
 }
 
 /************************** TEST_THE_SPECIAL_DATA **********************************/
 
 int test_the_special_data(double *parray, int itemcnt, double nyquist)
 {
-	double *p;
-	int isamp = 0, linecnt = 1;
-	p = parray;
-	while(linecnt <= itemcnt/2) {
-		if(isamp) {
-			if(*p < 0.0 || *p > 1.0) {
-				fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
-				return(DATA_ERROR);
-			}
-		} else {
-			if(*p < 0.0 || *p > nyquist) {
-				fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
-				return(DATA_ERROR);
-			}
-		}
-		if(!isamp)
-			linecnt++;
-		isamp = !isamp;
-		p++;
-	}
-	return(FINISHED);
+    double *p;
+    int isamp = 0, linecnt = 1;
+    p = parray;
+    while(linecnt <= itemcnt/2) {
+        if(isamp) {
+            if(*p < 0.0 || *p > 1.0) {
+                fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
+                return(DATA_ERROR);
+            }
+        } else {
+            if(*p < 0.0 || *p > nyquist) {
+                fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
+                return(DATA_ERROR);
+            }
+        }
+        if(!isamp)
+            linecnt++;
+        isamp = !isamp;
+        p++;
+    }
+    return(FINISHED);
 }
 
 /************************** PEAKISO **********************************/
 
 int peakiso(double *parray,int itemcnt,double *outarray,double minnotch)
 {
-	int ampat, trofampat, pkampbelowat, pkampaboveat, nexttrofat, writeat, j;
-	double amp, lastamp, ampstep, lastampstep, belowstep, abovestep, notchdepth;
-	double *pkbefore, *pkafter, *notch;
-	int trofcnt, badnotches, jj, kk, notchwidth, prepeak, postpeak, pksttej, pkendej;
-	int ampsttej, ampendej;
-	double sttval, endval;
-	int *pkbeforeat, *notchat, *orig_notchat;
+    int ampat, trofampat, pkampbelowat, pkampaboveat, nexttrofat, writeat, j;
+    double amp, lastamp, ampstep, lastampstep, belowstep, abovestep, notchdepth;
+    double *pkbefore, *pkafter, *notch;
+    int trofcnt, badnotches, jj, kk, notchwidth, prepeak, postpeak, pksttej, pkendej;
+    int ampsttej, ampendej;
+    double sttval, endval;
+    int *pkbeforeat, *notchat, *orig_notchat;
 
-	memset((char *)outarray,0,itemcnt * sizeof(double));
+    memset((char *)outarray,0,itemcnt * sizeof(double));
 
-		/* FIND PEAKS AND TROUGHS */
+        /* FIND PEAKS AND TROUGHS */
 
-	lastamp = parray[1];
-	lastampstep = 0;
-	ampat = 3;
-	trofcnt = 0;
-	while(ampat < itemcnt) {
-		amp = parray[ampat];
-		ampstep = amp - lastamp;
-		if(ampstep < 0) {		
-			if(lastampstep > 0) {			// falling after rise = peak
-				outarray[ampat-2] = 1.0;	//	peak
-			}
-			lastampstep = ampstep;
-		} else if(ampstep > 0) {
-			if(lastampstep < 0) {			// rising after fall = trough
-				outarray[ampat-2] = -1.0;	//	trough
-				trofcnt++;
-			}
-			lastampstep = ampstep;
-		}									//	else, if flat : lastampstep not updated
-		lastamp = amp;						//	saddle points are thus ignored, while flat peaks or troughs are spotted
-		ampat += 2;
-	}
-	if(trofcnt == 0) {
-		fprintf(stderr,"NO NOTCHES FOUND\n");
-		for(j = 0; j < itemcnt; j+=2) {
-			outarray[j] = parray[j];
-			outarray[j+1] = 0.0;
-		}
-		return FINISHED;
-	}
-	if((pkbefore = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
-		return(MEMORY_ERROR);
-	}
-	if((pkafter = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	if((pkbeforeat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
-		return(MEMORY_ERROR);
-	}
-	if((notch = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	if((notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	if((orig_notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
-		return(MEMORY_ERROR);
-	}
-	trofcnt = 0;
-	ampat = 1;
-	pkampbelowat = -1;						//	position of peak before trough
-	pkampaboveat = -1;						//	position of peak after trough
-	trofampat = -1;							//	position of trough
+    lastamp = parray[1];
+    lastampstep = 0;
+    ampat = 3;
+    trofcnt = 0;
+    while(ampat < itemcnt) {
+        amp = parray[ampat];
+        ampstep = amp - lastamp;
+        if(ampstep < 0) {       
+            if(lastampstep > 0) {           // falling after rise = peak
+                outarray[ampat-2] = 1.0;    //  peak
+            }
+            lastampstep = ampstep;
+        } else if(ampstep > 0) {
+            if(lastampstep < 0) {           // rising after fall = trough
+                outarray[ampat-2] = -1.0;   //  trough
+                trofcnt++;
+            }
+            lastampstep = ampstep;
+        }                                   //  else, if flat : lastampstep not updated
+        lastamp = amp;                      //  saddle points are thus ignored, while flat peaks or troughs are spotted
+        ampat += 2;
+    }
+    if(trofcnt == 0) {
+        fprintf(stderr,"NO NOTCHES FOUND\n");
+        for(j = 0; j < itemcnt; j+=2) {
+            outarray[j] = parray[j];
+            outarray[j+1] = 0.0;
+        }
+        return FINISHED;
+    }
+    if((pkbefore = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
+        return(MEMORY_ERROR);
+    }
+    if((pkafter = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    if((pkbeforeat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (1).\n");
+        return(MEMORY_ERROR);
+    }
+    if((notch = (double *)malloc((trofcnt + 6) * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    if((notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    if((orig_notchat = (int *)malloc((trofcnt + 6) * sizeof(int)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for trough calculations (2).\n");
+        return(MEMORY_ERROR);
+    }
+    trofcnt = 0;
+    ampat = 1;
+    pkampbelowat = -1;                      //  position of peak before trough
+    pkampaboveat = -1;                      //  position of peak after trough
+    trofampat = -1;                         //  position of trough
 
-	/* NOTE LEVELS BEFORE AND AFTER NOTCHES AND MARK BAD NOTCHES */
+    /* NOTE LEVELS BEFORE AND AFTER NOTCHES AND MARK BAD NOTCHES */
 
-	badnotches = 0;
+    badnotches = 0;
 
-	while(ampat < itemcnt) {
-		amp = outarray[ampat];
-		if(amp < 0.0) {
-			if(pkampbelowat < 0) {
-				pkampbelowat = 1;
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-			}
-			trofampat = ampat;
-			notch[trofcnt] = parray[ampat];
-			notchat[trofcnt] = ampat;
-		} else if(amp > 0.0) {				//	found peak
-			if(trofampat < 0) {				//	if not yet found trof, this is the peak below the trof
-				pkampbelowat = ampat;
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-			} else {						//	otherwise, its peak above trof : we now have complete notch
-				pkampaboveat = ampat;
-				pkafter[trofcnt] = parray[pkampaboveat];
-				belowstep = pkbefore[trofcnt] - parray[trofampat];
-				abovestep = pkafter[trofcnt]  - parray[trofampat];
-				notchdepth = min(belowstep,abovestep);
-				if(notchdepth < minnotch)
-					badnotches = 1;
-				trofcnt++;
-				pkampbelowat = pkampaboveat; //	move to next notch
-				pkbefore[trofcnt] = parray[pkampbelowat];
-				pkbeforeat[trofcnt] = pkampbelowat;
-				pkampaboveat = -1;
-				trofampat = -1;
-			}
-		}
-		ampat += 2;							//	if no peak found after last trough .. 
-	}										//	and last trof is not at very end (shoulf be impossible)
-	if(trofampat >= 0 && (trofampat != itemcnt-1) && (pkampaboveat < 0)) {
-		pkampaboveat = itemcnt - 1;			//	set peak to top edge of spectrum
-		pkafter[trofcnt]  = parray[pkampaboveat];
-		belowstep = pkbefore[trofcnt] - parray[trofampat];
-		abovestep = pkafter[trofcnt]  - parray[trofampat];
-		notchdepth = min(belowstep,abovestep);
-		if(notchdepth < minnotch)			//	Mark notches that are not deep enough
-			badnotches = 1;
-		trofcnt++;
-		pkbefore[trofcnt] = parray[pkampaboveat];	//	Extra "pkbefore" after last notch
-		pkbeforeat[trofcnt] = pkampaboveat;
-	}
-	for(kk=0;kk<trofcnt;kk++)
-		orig_notchat[kk] = notchat[kk];
+    while(ampat < itemcnt) {
+        amp = outarray[ampat];
+        if(amp < 0.0) {
+            if(pkampbelowat < 0) {
+                pkampbelowat = 1;
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+            }
+            trofampat = ampat;
+            notch[trofcnt] = parray[ampat];
+            notchat[trofcnt] = ampat;
+        } else if(amp > 0.0) {              //  found peak
+            if(trofampat < 0) {             //  if not yet found trof, this is the peak below the trof
+                pkampbelowat = ampat;
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+            } else {                        //  otherwise, its peak above trof : we now have complete notch
+                pkampaboveat = ampat;
+                pkafter[trofcnt] = parray[pkampaboveat];
+                belowstep = pkbefore[trofcnt] - parray[trofampat];
+                abovestep = pkafter[trofcnt]  - parray[trofampat];
+                notchdepth = min(belowstep,abovestep);
+                if(notchdepth < minnotch)
+                    badnotches = 1;
+                trofcnt++;
+                pkampbelowat = pkampaboveat; // move to next notch
+                pkbefore[trofcnt] = parray[pkampbelowat];
+                pkbeforeat[trofcnt] = pkampbelowat;
+                pkampaboveat = -1;
+                trofampat = -1;
+            }
+        }
+        ampat += 2;                         //  if no peak found after last trough .. 
+    }                                       //  and last trof is not at very end (shoulf be impossible)
+    if(trofampat >= 0 && (trofampat != itemcnt-1) && (pkampaboveat < 0)) {
+        pkampaboveat = itemcnt - 1;         //  set peak to top edge of spectrum
+        pkafter[trofcnt]  = parray[pkampaboveat];
+        belowstep = pkbefore[trofcnt] - parray[trofampat];
+        abovestep = pkafter[trofcnt]  - parray[trofampat];
+        notchdepth = min(belowstep,abovestep);
+        if(notchdepth < minnotch)           //  Mark notches that are not deep enough
+            badnotches = 1;
+        trofcnt++;
+        pkbefore[trofcnt] = parray[pkampaboveat];   //  Extra "pkbefore" after last notch
+        pkbeforeat[trofcnt] = pkampaboveat;
+    }
+    for(kk=0;kk<trofcnt;kk++)
+        orig_notchat[kk] = notchat[kk];
 
-	/* ELIMINATE ADJACENT BAD NOTCHES */
-		
-	if(badnotches) {
-		for(kk = 0;kk < trofcnt;kk++) {									//	x	      x
-			if(pkbefore[kk] - notch[kk]	< minnotch) {					//	 x   o   x
-				if((kk>0) && (pkbefore[kk] - notch[kk-1] < minnotch)) {	//    x x x x
-					//ELIMINATE PEAK									//	   x   o
-					outarray[pkbeforeat[kk]] = 0.0;
-					// KEEP DEEPEST NOTCH
-					if(notch[kk] <= notch[kk-1]) {					//		x	      x
-						outarray[notchat[kk-1]] = 0.0;				//		 x   o   x
-						notch[kk-1] = notch[kk];					//		  x x x x
-						notchat[kk-1]= notchat[kk];					//		   +   o			
-					} else {
-						outarray[notchat[kk]] = 0.0;
-					}
-					for(jj = kk+1;jj < trofcnt; jj++) {
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-						notch[jj-1] = notch[jj];
-						notchat[jj-1] = notchat[jj];
-					}
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-				} else if(pkbefore[kk+1] - notch[kk] < minnotch) {	//		   o   +
-					//ELIMINATE NOTCH								//		  x x x x
- 					outarray[notchat[kk]] = 0.0;					//		 x   o	 x
-					//ELIMINATE LOWEST PEAK							//		x		  x
-					if(pkbefore[kk] >= pkbefore[kk+1]) {
-						outarray[pkbeforeat[kk+1]] = 0.0;
-						if(kk < trofcnt-1) {
-							notch[kk] = notch[kk+1];
-							notchat[kk] = notchat[kk+1];
-						}
-						for(jj = kk+2;jj < trofcnt; jj++) {
-							pkbefore[jj-1] = pkbefore[jj];
-							pkbeforeat[jj-1] = pkbeforeat[jj];
-							notch[jj-1] = notch[jj];
-							notchat[jj-1] = notchat[jj];
-						}
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-					} else {
-						outarray[pkbeforeat[kk]] = 0.0;
-						for(jj = kk+1;jj < trofcnt; jj++) {
-							pkbefore[jj-1] = pkbefore[jj];
-							pkbeforeat[jj-1] = pkbeforeat[jj];
-							notch[jj-1] = notch[jj];
-							notchat[jj-1] = notchat[jj];
-						}
-						pkbefore[jj-1] = pkbefore[jj];
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-					}
-				} else {
-					//ELIMINATE PEAK BEFORE	AND NOTCH				//			x
-					outarray[pkbeforeat[kk]] = 0.0;					//	   o   x
-					outarray[notchat[kk]] = 0.0;					//	  x x x
-					for(jj = kk+1;jj < trofcnt; jj++) {				//	 x   o
-						pkbefore[jj-1] = pkbefore[jj];				//  x
-						pkbeforeat[jj-1] = pkbeforeat[jj];
-						notch[jj-1] = notch[jj];
-						notchat[jj-1] = notchat[jj];
-					}
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-				}
-				kk--;
-				trofcnt--;
-			} else if(pkbefore[kk+1] - notch[kk] < minnotch) {		//  x
-				//ELIMINATE PEAK AFTER AND NOTCH					//	 x   o
-				outarray[pkbeforeat[kk+1]] = 0.0;					//	  x x x
-				outarray[notchat[kk]] = 0.0;						//	   o   x
-				if(kk < trofcnt-1) {								//		    x
-					notch[kk] = notch[kk+1];
-					notchat[kk] = notchat[kk+1];
-				}
-				for(jj = kk+2;jj < trofcnt; jj++) {
-					pkbefore[jj-1] = pkbefore[jj];
-					pkbeforeat[jj-1] = pkbeforeat[jj];
-					notch[jj-1] = notch[jj];
-					notchat[jj-1] = notchat[jj];
-				}
-				pkbefore[jj-1] = pkbefore[jj];
-				pkbeforeat[jj-1] = pkbeforeat[jj];
-				kk--;
-				trofcnt--;
-			}
-		}
-	}
-	writeat = 0;							//	position of write to output
-	
-	/*	TRIM THE PEAKS */
-	if(trofcnt > 0) {
-		for(kk = 0;kk < trofcnt;kk++) {
-			nexttrofat = notchat[kk] - 1;	//	nexttrofat points to the PAIR of values: notchat points to its amplitude
-			if(kk == 0)
-				prepeak = pkbeforeat[kk];
-			else
-				prepeak = pkbeforeat[kk] - notchat[kk-1];
-			postpeak = notchat[kk] - pkbeforeat[kk];
-			notchwidth = min(prepeak,postpeak);
-			pksttej = (pkbeforeat[kk] - notchwidth) - 1;
-			ampsttej = pksttej + 1;
-			sttval = parray[ampsttej];
-			ampsttej += 2;
-			while(parray[ampsttej] < sttval) {		// avoid any curling up again at peak edges
-				sttval = parray[ampsttej] ;
-				ampsttej += 2;
-			}
-			ampsttej -= 2;
-			pksttej = ampsttej - 1;
-			if(pksttej == pkendej)					//	Avoid peaks overlapping each other
-				pksttej += 2;
-			pkendej = (pkbeforeat[kk] + notchwidth) - 1;
-			ampendej = pkendej + 1;
-			endval = parray[ampendej];
-			ampendej -= 2;
-			while(parray[ampendej] < endval) {		// avoid any curling up again at peak edges
-				endval = parray[ampendej] ;
-				ampendej -= 2;
-			}
-			ampendej += 2;
-			pkendej = ampendej - 1;
-			while(writeat < pksttej) {
-				outarray[writeat] = parray[writeat];
-				writeat++;
-				outarray[writeat] = 0.0;
-				writeat++;
-			}
-			while(writeat < pkendej) {
-				outarray[writeat] = parray[writeat];
-				writeat++;
-				outarray[writeat] = parray[writeat];
-				writeat++;
-			}
-			while(writeat < nexttrofat) {
-				outarray[writeat] = parray[writeat];
-				writeat++;
-				outarray[writeat] = 0.0;
-				writeat++;
-			}
-		}
-		nexttrofat = itemcnt;
-		prepeak = pkbeforeat[kk] - notchat[kk-1];
-		postpeak = (itemcnt + 1) - pkbeforeat[kk];
-		notchwidth = min(prepeak,postpeak);
-		pksttej = (pkbeforeat[kk] - notchwidth) - 1;
-		if(pksttej == pkendej)
-			pksttej += 2;
-		pkendej = (pkbeforeat[kk] + notchwidth) - 1;
-		while(writeat <pksttej) {
-			outarray[writeat] = parray[writeat];
-			writeat++;
-			outarray[writeat] = 0.0;
-			writeat++;
-		}
-		while(writeat <pkendej) {
-			outarray[writeat] = parray[writeat];
-			writeat++;
-			outarray[writeat] = parray[writeat];
-			writeat++;
-		}
-		while(writeat < nexttrofat) {
-			outarray[writeat] = parray[writeat];
-			writeat++;
-			outarray[writeat] = 0.0;
-			writeat++;
-		}
-	} else {
-		while(writeat < itemcnt) {
-			outarray[writeat] = parray[writeat];
-			writeat++;
-			outarray[writeat] = 0.0;
-			writeat++;
-		}
-	}
-	return(FINISHED);
+    /* ELIMINATE ADJACENT BAD NOTCHES */
+        
+    if(badnotches) {
+        for(kk = 0;kk < trofcnt;kk++) {                                 //  x         x
+            if(pkbefore[kk] - notch[kk] < minnotch) {                   //   x   o   x
+                if((kk>0) && (pkbefore[kk] - notch[kk-1] < minnotch)) { //    x x x x
+                    //ELIMINATE PEAK                                    //     x   o
+                    outarray[pkbeforeat[kk]] = 0.0;
+                    // KEEP DEEPEST NOTCH
+                    if(notch[kk] <= notch[kk-1]) {                  //      x         x
+                        outarray[notchat[kk-1]] = 0.0;              //       x   o   x
+                        notch[kk-1] = notch[kk];                    //        x x x x
+                        notchat[kk-1]= notchat[kk];                 //         +   o            
+                    } else {
+                        outarray[notchat[kk]] = 0.0;
+                    }
+                    for(jj = kk+1;jj < trofcnt; jj++) {
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                        notch[jj-1] = notch[jj];
+                        notchat[jj-1] = notchat[jj];
+                    }
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                } else if(pkbefore[kk+1] - notch[kk] < minnotch) {  //         o   +
+                    //ELIMINATE NOTCH                               //        x x x x
+                    outarray[notchat[kk]] = 0.0;                    //       x   o   x
+                    //ELIMINATE LOWEST PEAK                         //      x         x
+                    if(pkbefore[kk] >= pkbefore[kk+1]) {
+                        outarray[pkbeforeat[kk+1]] = 0.0;
+                        if(kk < trofcnt-1) {
+                            notch[kk] = notch[kk+1];
+                            notchat[kk] = notchat[kk+1];
+                        }
+                        for(jj = kk+2;jj < trofcnt; jj++) {
+                            pkbefore[jj-1] = pkbefore[jj];
+                            pkbeforeat[jj-1] = pkbeforeat[jj];
+                            notch[jj-1] = notch[jj];
+                            notchat[jj-1] = notchat[jj];
+                        }
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                    } else {
+                        outarray[pkbeforeat[kk]] = 0.0;
+                        for(jj = kk+1;jj < trofcnt; jj++) {
+                            pkbefore[jj-1] = pkbefore[jj];
+                            pkbeforeat[jj-1] = pkbeforeat[jj];
+                            notch[jj-1] = notch[jj];
+                            notchat[jj-1] = notchat[jj];
+                        }
+                        pkbefore[jj-1] = pkbefore[jj];
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                    }
+                } else {
+                    //ELIMINATE PEAK BEFORE AND NOTCH               //          x
+                    outarray[pkbeforeat[kk]] = 0.0;                 //     o   x
+                    outarray[notchat[kk]] = 0.0;                    //    x x x
+                    for(jj = kk+1;jj < trofcnt; jj++) {             //   x   o
+                        pkbefore[jj-1] = pkbefore[jj];              //  x
+                        pkbeforeat[jj-1] = pkbeforeat[jj];
+                        notch[jj-1] = notch[jj];
+                        notchat[jj-1] = notchat[jj];
+                    }
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                }
+                kk--;
+                trofcnt--;
+            } else if(pkbefore[kk+1] - notch[kk] < minnotch) {      //  x
+                //ELIMINATE PEAK AFTER AND NOTCH                    //   x   o
+                outarray[pkbeforeat[kk+1]] = 0.0;                   //    x x x
+                outarray[notchat[kk]] = 0.0;                        //     o   x
+                if(kk < trofcnt-1) {                                //          x
+                    notch[kk] = notch[kk+1];
+                    notchat[kk] = notchat[kk+1];
+                }
+                for(jj = kk+2;jj < trofcnt; jj++) {
+                    pkbefore[jj-1] = pkbefore[jj];
+                    pkbeforeat[jj-1] = pkbeforeat[jj];
+                    notch[jj-1] = notch[jj];
+                    notchat[jj-1] = notchat[jj];
+                }
+                pkbefore[jj-1] = pkbefore[jj];
+                pkbeforeat[jj-1] = pkbeforeat[jj];
+                kk--;
+                trofcnt--;
+            }
+        }
+    }
+    writeat = 0;                            //  position of write to output
+    
+    /*  TRIM THE PEAKS */
+    if(trofcnt > 0) {
+        for(kk = 0;kk < trofcnt;kk++) {
+            nexttrofat = notchat[kk] - 1;   //  nexttrofat points to the PAIR of values: notchat points to its amplitude
+            if(kk == 0)
+                prepeak = pkbeforeat[kk];
+            else
+                prepeak = pkbeforeat[kk] - notchat[kk-1];
+            postpeak = notchat[kk] - pkbeforeat[kk];
+            notchwidth = min(prepeak,postpeak);
+            pksttej = (pkbeforeat[kk] - notchwidth) - 1;
+            ampsttej = pksttej + 1;
+            sttval = parray[ampsttej];
+            ampsttej += 2;
+            while(parray[ampsttej] < sttval) {      // avoid any curling up again at peak edges
+                sttval = parray[ampsttej] ;
+                ampsttej += 2;
+            }
+            ampsttej -= 2;
+            pksttej = ampsttej - 1;
+            if(pksttej == pkendej)                  //  Avoid peaks overlapping each other
+                pksttej += 2;
+            pkendej = (pkbeforeat[kk] + notchwidth) - 1;
+            ampendej = pkendej + 1;
+            endval = parray[ampendej];
+            ampendej -= 2;
+            while(parray[ampendej] < endval) {      // avoid any curling up again at peak edges
+                endval = parray[ampendej] ;
+                ampendej -= 2;
+            }
+            ampendej += 2;
+            pkendej = ampendej - 1;
+            while(writeat < pksttej) {
+                outarray[writeat] = parray[writeat];
+                writeat++;
+                outarray[writeat] = 0.0;
+                writeat++;
+            }
+            while(writeat < pkendej) {
+                outarray[writeat] = parray[writeat];
+                writeat++;
+                outarray[writeat] = parray[writeat];
+                writeat++;
+            }
+            while(writeat < nexttrofat) {
+                outarray[writeat] = parray[writeat];
+                writeat++;
+                outarray[writeat] = 0.0;
+                writeat++;
+            }
+        }
+        nexttrofat = itemcnt;
+        prepeak = pkbeforeat[kk] - notchat[kk-1];
+        postpeak = (itemcnt + 1) - pkbeforeat[kk];
+        notchwidth = min(prepeak,postpeak);
+        pksttej = (pkbeforeat[kk] - notchwidth) - 1;
+        if(pksttej == pkendej)
+            pksttej += 2;
+        pkendej = (pkbeforeat[kk] + notchwidth) - 1;
+        while(writeat <pksttej) {
+            outarray[writeat] = parray[writeat];
+            writeat++;
+            outarray[writeat] = 0.0;
+            writeat++;
+        }
+        while(writeat <pkendej) {
+            outarray[writeat] = parray[writeat];
+            writeat++;
+            outarray[writeat] = parray[writeat];
+            writeat++;
+        }
+        while(writeat < nexttrofat) {
+            outarray[writeat] = parray[writeat];
+            writeat++;
+            outarray[writeat] = 0.0;
+            writeat++;
+        }
+    } else {
+        while(writeat < itemcnt) {
+            outarray[writeat] = parray[writeat];
+            writeat++;
+            outarray[writeat] = 0.0;
+            writeat++;
+        }
+    }
+    return(FINISHED);
 }
 
 /************************** GET_FLOAT_WITH_E_FROM_WITHIN_STRING **************************
@@ -510,43 +533,43 @@ int peakiso(double *parray,int itemcnt,double *outarray,double minnotch)
 
 int  get_float_with_e_from_within_string(char **str,double *val)
 {
-	char   *p, *valstart;
-	int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
-	p = *str;
-	while(isspace(*p))
-		p++;
-	valstart = p;	
-	switch(*p) {
-	case('-'):						break;
-	case('.'): decimal_point_cnt=1;	break;
-	default:
-		if(!isdigit(*p))
-			return(FALSE);
-		has_digits = TRUE;
-		break;
-	}
-	p++;
-	while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
-		if(isdigit(*p))
-			has_digits = TRUE;
-		else if(*p == 'e') {
-			if(has_e || !has_digits)
-				return(FALSE);
-			has_e = 1;
-		} else if(*p == '-') {
-			if(!has_e || (lastchar != 'e'))
-				return(FALSE);
-		} else if(*p == '.') {
-			if(has_e || (++decimal_point_cnt>1))
-				return(FALSE);
-		} else
-			return(FALSE);
-		lastchar = *p;
-		p++;
-	}
-	if(!has_digits || sscanf(valstart,"%lf",val)!=1)
-		return(FALSE);
-	*str = p;
-	return(TRUE);
+    char   *p, *valstart;
+    int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
+    p = *str;
+    while(isspace(*p))
+        p++;
+    valstart = p;   
+    switch(*p) {
+    case('-'):                      break;
+    case('.'): decimal_point_cnt=1; break;
+    default:
+        if(!isdigit(*p))
+            return(FALSE);
+        has_digits = TRUE;
+        break;
+    }
+    p++;
+    while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
+        if(isdigit(*p))
+            has_digits = TRUE;
+        else if(*p == 'e') {
+            if(has_e || !has_digits)
+                return(FALSE);
+            has_e = 1;
+        } else if(*p == '-') {
+            if(!has_e || (lastchar != 'e'))
+                return(FALSE);
+        } else if(*p == '.') {
+            if(has_e || (++decimal_point_cnt>1))
+                return(FALSE);
+        } else
+            return(FALSE);
+        lastchar = *p;
+        p++;
+    }
+    if(!has_digits || sscanf(valstart,"%lf",val)!=1)
+        return(FALSE);
+    *str = p;
+    return(TRUE);
 }
 

+ 1 - 1
dev/science/pulser.c

@@ -47,7 +47,7 @@
 #include <limits.h>
 
 
-static int testflag;
+//static int testflag;
 
 #if defined unix || defined __GNUC__
 #define round(x) lround((x))

+ 346 - 322
dev/science/smooth.c

@@ -1,3 +1,27 @@
+/*
+ * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
+ * http://www.trevorwishart.co.uk
+ * http://www.composersdesktop.com
+ *
+ This file is part of the CDP System.
+ 
+ The CDP System 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.
+ 
+ The CDP System 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 the CDP System; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA
+ *
+ */
+
 /*
  * Take a set of amp-frq data in the correct range,
  * and use linear interpolation to replot curve to cover
@@ -6,7 +30,7 @@
  * use chan centre frqs.
  */
 
-#define PSEUDOCENT	(1.0009)	// approx 1/64th of a tone
+#define PSEUDOCENT  (1.0009)    // approx 1/64th of a tone
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,14 +52,14 @@
 #include <string.h>
 #include <srates.h>
 
-int	sloom = 0;
+int sloom = 0;
 int sloombatch = 0;
 const char* cdp_version = "6.1.0";
 
 char errstr[2400];  //RWD need this
 //CDP LIB REPLACEMENTS
 static int allocate_output_array(double **outarray, int *outcnt, double chwidth, double halfchwidth, double nyquist);
-static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
+//static int get_tk_cmdline_word(int *cmdlinecnt,char ***cmdline,char *q);
 static int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt);
 static int test_the_special_data(double *parray, int itemcnt, double nyquist);
 static int smooth(double *parray,int itemcnt,double *outarray,int *outcnt,double chwidth,double halfchwidth,double nyquist,int arraycnt);
@@ -47,87 +71,87 @@ static int  get_float_with_e_from_within_string(char **str,double *val);
 
 int main(int argc,char *argv[])
 {
-	FILE *fpi, *fpo;
-	int k, OK = 0, dovetail = 0;
-	int exit_status, srate, pointcnt, clength;
-	double chwidth, halfchwidth, nyquist, *parray, *outarray;
-	int itemcnt, outcnt, arraycnt;
-	char temp[200], temp2[200];
-	if(argc < 5 || argc > 6) {
-		usage1();	
-		return(FAILED);
-	}
-	if((fpi = fopen(argv[1],"r")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
-		return(FAILED);
-	}
-	if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
-		return(FAILED);
-	arraycnt = itemcnt/2;
-	if(sscanf(argv[3],"%d",&pointcnt) != 1) {
-		fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
-		return(FAILED);
-	}
-	OK = 0;
-	k = 1;
-	while(k < pointcnt) {
-		k *= 2;
-		if(k == pointcnt){
-			OK= 1;
-			break;
-		}
-	}
-	if(!OK) {
-		fprintf(stderr,"ANALYSIS POINTS PARAMETER MUST BE A POWER OF TWO.\n");
-		return(FAILED);
-	}
-	if(sscanf(argv[4],"%d",&srate) != 1) {
-		fprintf(stderr,"CANNOT READ SAMPLE RATE (%s)\n",argv[3]);
-		return(FAILED);
-	}
- 	if(srate < 44100 || BAD_SR(srate)) {
-		fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
-		return(DATA_ERROR);
-	}
-	if(argc == 6) {
-		if(strcmp(argv[5],"-s")) {
-			fprintf(stderr,"UNKNOWN FINAL PARAMETER (%s).\n",argv[5]);
-			return(DATA_ERROR);
-		}
-		dovetail = 1;
-	}
-	clength	 = (pointcnt + 2)/2;
-	nyquist	 = (double)(srate / 2);		
-	chwidth	 = nyquist/(double)clength;
-	halfchwidth = chwidth / 2;
+    FILE *fpi, *fpo;
+    int k, OK = 0, dovetail = 0;
+    int exit_status, srate, pointcnt, clength;
+    double chwidth, halfchwidth, nyquist, *parray, *outarray;
+    int itemcnt, outcnt, arraycnt;
+    char temp[200], temp2[200];
+    if(argc < 5 || argc > 6) {
+        usage1();   
+        return(FAILED);
+    }
+    if((fpi = fopen(argv[1],"r")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN DATAFILE %s\n",argv[1]);
+        return(FAILED);
+    }
+    if((exit_status = handle_the_special_data(fpi,&parray,&itemcnt))<0)
+        return(FAILED);
+    arraycnt = itemcnt/2;
+    if(sscanf(argv[3],"%d",&pointcnt) != 1) {
+        fprintf(stderr,"CANNOT READ POINTCNT (%s)\n",argv[3]);
+        return(FAILED);
+    }
+    OK = 0;
+    k = 1;
+    while(k < pointcnt) {
+        k *= 2;
+        if(k == pointcnt){
+            OK= 1;
+            break;
+        }
+    }
+    if(!OK) {
+        fprintf(stderr,"ANALYSIS POINTS PARAMETER MUST BE A POWER OF TWO.\n");
+        return(FAILED);
+    }
+    if(sscanf(argv[4],"%d",&srate) != 1) {
+        fprintf(stderr,"CANNOT READ SAMPLE RATE (%s)\n",argv[3]);
+        return(FAILED);
+    }
+    if(srate < 44100 || BAD_SR(srate)) {
+        fprintf(stderr,"INVALID SAMPLE RATE ENTERED (44100,48000,88200,96000 only).\n");
+        return(DATA_ERROR);
+    }
+    if(argc == 6) {
+        if(strcmp(argv[5],"-s")) {
+            fprintf(stderr,"UNKNOWN FINAL PARAMETER (%s).\n",argv[5]);
+            return(DATA_ERROR);
+        }
+        dovetail = 1;
+    }
+    clength  = (pointcnt + 2)/2;
+    nyquist  = (double)(srate / 2);     
+    chwidth  = nyquist/(double)clength;
+    halfchwidth = chwidth / 2;
 
-	if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
-		return(FAILED);
-	if((exit_status = allocate_output_array(&outarray,&outcnt,chwidth,halfchwidth,nyquist))<0)
-		return(FAILED);
-	if((fpo = fopen(argv[2],"w")) == NULL) {
-		fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
-		return(FAILED);
-	}
-	if((exit_status = smooth(parray,itemcnt,outarray,&outcnt,chwidth,halfchwidth,nyquist,arraycnt)) < 0)
-		return(FAILED);
-	if(dovetail) {
-		if((exit_status = dove(parray,itemcnt,outarray,outcnt)) < 0)
-			return(FAILED);
-	}
-	k = 0;
-	for(k = 0;k < outcnt;k+=2) {
-		sprintf(temp,"%lf",outarray[k]);
-		sprintf(temp2,"%lf",outarray[k+1]);
-		strcat(temp2,"\n");
-		strcat(temp,"\t");
-		strcat(temp,temp2);
-		fputs(temp,fpo);
-	}
-	if(fclose(fpo)<0) {
-		fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
-	}
-	return(SUCCEEDED);
+    if((exit_status = test_the_special_data(parray,itemcnt,nyquist))<0)
+        return(FAILED);
+    if((exit_status = allocate_output_array(&outarray,&outcnt,chwidth,halfchwidth,nyquist))<0)
+        return(FAILED);
+    if((fpo = fopen(argv[2],"w")) == NULL) {
+        fprintf(stderr,"CANNOT OPEN OUTPUT DATAFILE %s\n",argv[2]);
+        return(FAILED);
+    }
+    if((exit_status = smooth(parray,itemcnt,outarray,&outcnt,chwidth,halfchwidth,nyquist,arraycnt)) < 0)
+        return(FAILED);
+    if(dovetail) {
+        if((exit_status = dove(parray,itemcnt,outarray,outcnt)) < 0)
+            return(FAILED);
+    }
+    k = 0;
+    for(k = 0;k < outcnt;k+=2) {
+        sprintf(temp,"%lf",outarray[k]);
+        sprintf(temp2,"%lf",outarray[k+1]);
+        strcat(temp2,"\n");
+        strcat(temp,"\t");
+        strcat(temp,temp2);
+        fputs(temp,fpo);
+    }
+    if(fclose(fpo)<0) {
+        fprintf(stderr,"WARNING: Failed to close output data file %s.\n",argv[2]);
+    }
+    return(SUCCEEDED);
 }
 
 
@@ -135,207 +159,207 @@ int main(int argc,char *argv[])
 
 int usage1(void)
 {
-	fprintf(stderr,
-	"USAGE: smooth datafile outdatafile pointcnt srate [-s]\n"
-	"\n"
-	"Generate smoothed curve from input data re spectrum.\n"
-	"\n"
-	"DATAFILE     textfile of frq/amp pairs.\n"
-	"OUTDATAFILE  textfile of smoothed data.\n"
-	"POINTCNT     no of analysis points required in handling data.\n"
-	"SRATE        samplerate of eventual sound output.\n"
-	"-s           if spectral amp falls to zero before the nyquist,\n"
-	"             keep zeroed end of spectrum at zero.\n."
-	"\n");
-	return(USAGE_ONLY);
+    fprintf(stderr,
+    "USAGE: smooth datafile outdatafile pointcnt srate [-s]\n"
+    "\n"
+    "Generate smoothed curve from input data re spectrum.\n"
+    "\n"
+    "DATAFILE     textfile of frq/amp pairs.\n"
+    "OUTDATAFILE  textfile of smoothed data.\n"
+    "POINTCNT     no of analysis points required in handling data.\n"
+    "SRATE        samplerate of eventual sound output.\n"
+    "-s           if spectral amp falls to zero before the nyquist,\n"
+    "             keep zeroed end of spectrum at zero.\n."
+    "\n");
+    return(USAGE_ONLY);
 }
 
 /************************** HANDLE_THE_SPECIAL_DATA **********************************/
 
 int handle_the_special_data(FILE *fpi, double **parray, int *itemcnt)
 {
-	int cnt, linecnt;
-	double *p, dummy;
-	char temp[200], *q;
-	cnt = 0;
-	p = &dummy;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p))
-			cnt++;
-	}
-	if(ODD(cnt)) {
-		fprintf(stderr,"Data not paired correctly in input data file\n");
-		return(DATA_ERROR);
-	}
-	if(cnt == 0) {
-		fprintf(stderr,"No data in input data file\n");
-		return(DATA_ERROR);
-	}
-	if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	fseek(fpi,0,0);
-	linecnt = 1;
-	p = *parray;
-	while(fgets(temp,200,fpi)==temp) {
-		q = temp;
-		if(*q == ';')	//	Allow comments in file
-			continue;
-		while(get_float_with_e_from_within_string(&q,p)) {
-			p++;
-		}
-		linecnt++;
-	}
-	if(fclose(fpi)<0) {
-		fprintf(stderr,"WARNING: Failed to close input data file.\n");
-	}
-	*itemcnt = cnt;
-	return(FINISHED);
+    int cnt, linecnt;
+    double *p, dummy;
+    char temp[200], *q;
+    cnt = 0;
+    p = &dummy;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p))
+            cnt++;
+    }
+    if(ODD(cnt)) {
+        fprintf(stderr,"Data not paired correctly in input data file\n");
+        return(DATA_ERROR);
+    }
+    if(cnt == 0) {
+        fprintf(stderr,"No data in input data file\n");
+        return(DATA_ERROR);
+    }
+    if((*parray = (double *)malloc(cnt * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    fseek(fpi,0,0);
+    linecnt = 1;
+    p = *parray;
+    while(fgets(temp,200,fpi)==temp) {
+        q = temp;
+        if(*q == ';')   //  Allow comments in file
+            continue;
+        while(get_float_with_e_from_within_string(&q,p)) {
+            p++;
+        }
+        linecnt++;
+    }
+    if(fclose(fpi)<0) {
+        fprintf(stderr,"WARNING: Failed to close input data file.\n");
+    }
+    *itemcnt = cnt;
+    return(FINISHED);
 }
 
 /************************** TEST_THE_SPECIAL_DATA **********************************/
 
 int test_the_special_data(double *parray, int itemcnt, double nyquist)
 {
-	double *p;
-	int isamp = 0, linecnt = 1;
-	p = parray;
-	while(linecnt <= itemcnt/2) {
-		if(isamp) {
-			if(*p < 0.0 || *p > 1.0) {
-				fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
-				return(DATA_ERROR);
-			}
-		} else {
-			if(*p < 0.0 || *p > nyquist) {
-				fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
-				return(DATA_ERROR);
-			}
-		}
-		if(!isamp)
-			linecnt++;
-		isamp = !isamp;
-		p++;
-	}
-	return(FINISHED);
+    double *p;
+    int isamp = 0, linecnt = 1;
+    p = parray;
+    while(linecnt <= itemcnt/2) {
+        if(isamp) {
+            if(*p < 0.0 || *p > 1.0) {
+                fprintf(stderr,"Amp (%lf) out of range (0-1) at line %d in datafile.\n",*p,linecnt);
+                return(DATA_ERROR);
+            }
+        } else {
+            if(*p < 0.0 || *p > nyquist) {
+                fprintf(stderr,"Frq (%lf) out of range (0 - %lf) at line %d in datafile.\n",*p,nyquist,linecnt);
+                return(DATA_ERROR);
+            }
+        }
+        if(!isamp)
+            linecnt++;
+        isamp = !isamp;
+        p++;
+    }
+    return(FINISHED);
 }
 
 /************************** ALLOCATE_OUTPUT_ARRAY **********************************/
 
 int allocate_output_array(double **outarray,int *outcnt,double chwidth,double halfchwidth,double nyquist)
 {
-	int outsize = 1;  // Count initial halfwidht channel
-	double lastlofrq, lofrq = halfchwidth;
-	while(lofrq <= nyquist) {
-		outsize++;
-		lastlofrq = lofrq;	
-		lofrq *= PSEUDOCENT;
-		if(lofrq - lastlofrq >= chwidth)	//	Once chan separation is > chwidth
-			lofrq = lastlofrq + chwidth;	//	Just set 1 value per channel
-	}
-	outsize += 64;  //	SAFETY
-	outsize *= 2;
-	if((*outarray = (double *)malloc(outsize * sizeof(double)))==NULL) {
-		fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
-		return(MEMORY_ERROR);
-	}
-	*outcnt = outsize;
-	return(	FINISHED);
+    int outsize = 1;  // Count initial halfwidht channel
+    double lastlofrq, lofrq = halfchwidth;
+    while(lofrq <= nyquist) {
+        outsize++;
+        lastlofrq = lofrq;  
+        lofrq *= PSEUDOCENT;
+        if(lofrq - lastlofrq >= chwidth)    //  Once chan separation is > chwidth
+            lofrq = lastlofrq + chwidth;    //  Just set 1 value per channel
+    }
+    outsize += 64;  //  SAFETY
+    outsize *= 2;
+    if((*outarray = (double *)malloc(outsize * sizeof(double)))==NULL) {
+        fprintf(stderr,"INSUFFICIENT MEMORY for input data.\n");
+        return(MEMORY_ERROR);
+    }
+    *outcnt = outsize;
+    return( FINISHED);
 }
 
 /************************** SMOOTH **********************************/
 
 int smooth(double *parray,int itemcnt,double *outarray,int *outcnt,double chwidth,double halfchwidth,double nyquist,int arraycnt)
 {
-	int exit_status;
-	int k = 0, hi = 0;
-	double lastlofrq, lofrq = halfchwidth, thisamp;
-	double *brkptr = parray;
-	int brksize = itemcnt/2;
-	double firstval,lastval,lastind;
-	int brkinit = 0;
-	outarray[k] = 0.0;
-	outarray[k+1] = 0.0;
-	k = 2;
-	while(lofrq < nyquist) {
-		if(k >= *outcnt) {
-			fprintf(stderr,"OUT OF MEMORY IN OUTPUT ARRAY.\n");
-			return(MEMORY_ERROR);
-		}
-		if((exit_status = read_value_from_array(&thisamp,lofrq,parray,&brkptr,brksize,&firstval,&lastval,&lastind,&brkinit))<0)
-			return(exit_status);
-		outarray[k] = lofrq;
-		outarray[k+1] = thisamp;
-		lastlofrq = lofrq;	
-		lofrq *= PSEUDOCENT;
-		if(lofrq - lastlofrq >= chwidth)	//	Once chan separation is < PSEUDOCENT
-			lofrq = lastlofrq + chwidth;	//	Just set 1 value per channel
-		k += 2;
-	}
-	outarray[k++] = nyquist;
-	outarray[k++] = 0.0;
-	*outcnt = k;
-	if(outarray[0] == 0.0 && outarray[2] == 0.0) {		// Eliminate any duplication of zero start frq
-		k = 0;
-		hi = 2;
-		while(hi < *outcnt) {
-			outarray[k] = outarray[hi];
-			k++;
-			hi++;
-		}
-		*outcnt = k;
-	}	
-	return(FINISHED);
+    int exit_status;
+    int k = 0, hi = 0;
+    double lastlofrq, lofrq = halfchwidth, thisamp;
+    double *brkptr = parray;
+    int brksize = itemcnt/2;
+    double firstval,lastval,lastind;
+    int brkinit = 0;
+    outarray[k] = 0.0;
+    outarray[k+1] = 0.0;
+    k = 2;
+    while(lofrq < nyquist) {
+        if(k >= *outcnt) {
+            fprintf(stderr,"OUT OF MEMORY IN OUTPUT ARRAY.\n");
+            return(MEMORY_ERROR);
+        }
+        if((exit_status = read_value_from_array(&thisamp,lofrq,parray,&brkptr,brksize,&firstval,&lastval,&lastind,&brkinit))<0)
+            return(exit_status);
+        outarray[k] = lofrq;
+        outarray[k+1] = thisamp;
+        lastlofrq = lofrq;  
+        lofrq *= PSEUDOCENT;
+        if(lofrq - lastlofrq >= chwidth)    //  Once chan separation is < PSEUDOCENT
+            lofrq = lastlofrq + chwidth;    //  Just set 1 value per channel
+        k += 2;
+    }
+    outarray[k++] = nyquist;
+    outarray[k++] = 0.0;
+    *outcnt = k;
+    if(outarray[0] == 0.0 && outarray[2] == 0.0) {      // Eliminate any duplication of zero start frq
+        k = 0;
+        hi = 2;
+        while(hi < *outcnt) {
+            outarray[k] = outarray[hi];
+            k++;
+            hi++;
+        }
+        *outcnt = k;
+    }   
+    return(FINISHED);
 }
 
 /************************** DOVE **********************************/
 
 int dove(double *parray,int itemcnt,double *outarray,int outcnt)
 {
-	double *amp, *frq, *inarrayend = parray + itemcnt, *outarrayend = outarray + outcnt, tailstartfrq;
-	amp = inarrayend - 1;
-	if(!flteq(*amp,0.0))		// Input spectrum never falls to zero
-		return(FINISHED);
+    double *amp, *frq, *inarrayend = parray + itemcnt, *outarrayend = outarray + outcnt, tailstartfrq;
+    amp = inarrayend - 1;
+    if(!flteq(*amp,0.0))        // Input spectrum never falls to zero
+        return(FINISHED);
 
-	while(amp > parray) {		//	Search back from end of (orig) spectrum, until spectral vals are >0
-		if(!flteq(*amp,0.0))
-			break;
-		amp -= 2;
-	}
-	if(amp <= parray)			//	(original) spectrum is everywhere zero
-		return(FINISHED);
-	frq = amp - 1;
-	tailstartfrq = *frq;		//	Note frq at which (original) spectrum finally falls to zero
+    while(amp > parray) {       //  Search back from end of (orig) spectrum, until spectral vals are >0
+        if(!flteq(*amp,0.0))
+            break;
+        amp -= 2;
+    }
+    if(amp <= parray)           //  (original) spectrum is everywhere zero
+        return(FINISHED);
+    frq = amp - 1;
+    tailstartfrq = *frq;        //  Note frq at which (original) spectrum finally falls to zero
 
-	frq = outarray;				//	Parse frq vals in output array
-	while(frq < outarrayend) {
-		if(*frq >= tailstartfrq) {
-			amp = frq - 1;		//	Note where outfrq exceeds tailstartfrq of input, and step to amp BEFORE it
-			break;
-		}
-		frq += 2;
-	}
-	if(frq >= outarrayend) {	//	Output frq never exceeds input frq: should be impossible
-		fprintf(stderr,"DOVETAILING FAILED.\n");
-		return(PROGRAM_ERROR);
-	}
-	while(amp < outarrayend) {	//	Search for point where output first touches zero
-		if(flteq(*amp,0.0))
-			break;
-		amp += 2;
-	}
-	if(amp >= outarrayend) {
-		fprintf(stderr,"NO DOVETAILING OCCURED.\n");
-	}
-	while(amp < outarrayend) {	//	Set tail to zero
-		*amp = 0.0;
-		amp += 2;
-	}
-	return(FINISHED);
+    frq = outarray;             //  Parse frq vals in output array
+    while(frq < outarrayend) {
+        if(*frq >= tailstartfrq) {
+            amp = frq - 1;      //  Note where outfrq exceeds tailstartfrq of input, and step to amp BEFORE it
+            break;
+        }
+        frq += 2;
+    }
+    if(frq >= outarrayend) {    //  Output frq never exceeds input frq: should be impossible
+        fprintf(stderr,"DOVETAILING FAILED.\n");
+        return(PROGRAM_ERROR);
+    }
+    while(amp < outarrayend) {  //  Search for point where output first touches zero
+        if(flteq(*amp,0.0))
+            break;
+        amp += 2;
+    }
+    if(amp >= outarrayend) {
+        fprintf(stderr,"NO DOVETAILING OCCURED.\n");
+    }
+    while(amp < outarrayend) {  //  Set tail to zero
+        *amp = 0.0;
+        amp += 2;
+    }
+    return(FINISHED);
 }
 
 /**************************** READ_VALUE_FROM_ARRAY *****************************/
@@ -345,38 +369,38 @@ int read_value_from_array(double *outval,double thisfrq,double *brk,double **brk
     double *endpair, *p, val;
     double hival, loval, hiind, loind;
     if(!(*brkinit)) {
-		*brkptr   = brk;
-		*firstval = *(brk+1);
-		endpair   = brk + ((brksize-1)*2);
-		*lastind  = *endpair;
-		*lastval  = *(endpair+1);
- 		*brkinit = 1;
-    }
-	p = *brkptr;
-	if(thisfrq <= *brk) {
-		*outval = *firstval;
-		return(FINISHED);
-	} else if(thisfrq >= *lastind) {
-		*outval = *lastval;
-		return(FINISHED);
-	} 
-	if(thisfrq > *p) {
-		while(*p < thisfrq)
-			p += 2;
-	} else {
-		while(*p >=thisfrq)
-			p -= 2;
-		p += 2;
-	}
-	hival  = *(p+1);
-	hiind  = *p;
-	loval  = *(p-1);
-	loind  = *(p-2);
-	val    = (thisfrq - loind)/(hiind - loind);
-	val   *= (hival - loval);
-	val   += loval;
-	*outval = val;
- 	*brkptr = p;
+        *brkptr   = brk;
+        *firstval = *(brk+1);
+        endpair   = brk + ((brksize-1)*2);
+        *lastind  = *endpair;
+        *lastval  = *(endpair+1);
+        *brkinit = 1;
+    }
+    p = *brkptr;
+    if(thisfrq <= *brk) {
+        *outval = *firstval;
+        return(FINISHED);
+    } else if(thisfrq >= *lastind) {
+        *outval = *lastval;
+        return(FINISHED);
+    } 
+    if(thisfrq > *p) {
+        while(*p < thisfrq)
+            p += 2;
+    } else {
+        while(*p >=thisfrq)
+            p -= 2;
+        p += 2;
+    }
+    hival  = *(p+1);
+    hiind  = *p;
+    loval  = *(p-1);
+    loind  = *(p-2);
+    val    = (thisfrq - loind)/(hiind - loind);
+    val   *= (hival - loval);
+    val   += loval;
+    *outval = val;
+    *brkptr = p;
     return(FINISHED);
 }
 
@@ -388,43 +412,43 @@ int read_value_from_array(double *outval,double thisfrq,double *brk,double **brk
 
 int  get_float_with_e_from_within_string(char **str,double *val)
 {
-	char   *p, *valstart;
-	int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
-	p = *str;
-	while(isspace(*p))
-		p++;
-	valstart = p;	
-	switch(*p) {
-	case('-'):						break;
-	case('.'): decimal_point_cnt=1;	break;
-	default:
-		if(!isdigit(*p))
-			return(FALSE);
-		has_digits = TRUE;
-		break;
-	}
-	p++;
-	while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
-		if(isdigit(*p))
-			has_digits = TRUE;
-		else if(*p == 'e') {
-			if(has_e || !has_digits)
-				return(FALSE);
-			has_e = 1;
-		} else if(*p == '-') {
-			if(!has_e || (lastchar != 'e'))
-				return(FALSE);
-		} else if(*p == '.') {
-			if(has_e || (++decimal_point_cnt>1))
-				return(FALSE);
-		} else
-			return(FALSE);
-		lastchar = *p;
-		p++;
-	}
-	if(!has_digits || sscanf(valstart,"%lf",val)!=1)
-		return(FALSE);
-	*str = p;
-	return(TRUE);
+    char   *p, *valstart;
+    int    decimal_point_cnt = 0, has_digits = 0, has_e = 0, lastchar = 0;
+    p = *str;
+    while(isspace(*p))
+        p++;
+    valstart = p;   
+    switch(*p) {
+    case('-'):                      break;
+    case('.'): decimal_point_cnt=1; break;
+    default:
+        if(!isdigit(*p))
+            return(FALSE);
+        has_digits = TRUE;
+        break;
+    }
+    p++;
+    while(!isspace(*p) && *p!=NEWLINE && *p!=ENDOFSTR) {
+        if(isdigit(*p))
+            has_digits = TRUE;
+        else if(*p == 'e') {
+            if(has_e || !has_digits)
+                return(FALSE);
+            has_e = 1;
+        } else if(*p == '-') {
+            if(!has_e || (lastchar != 'e'))
+                return(FALSE);
+        } else if(*p == '.') {
+            if(has_e || (++decimal_point_cnt>1))
+                return(FALSE);
+        } else
+            return(FALSE);
+        lastchar = *p;
+        p++;
+    }
+    if(!has_digits || sscanf(valstart,"%lf",val)!=1)
+        return(FALSE);
+    *str = p;
+    return(TRUE);
 }
 

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio