home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- #include <stdio.h>
- // #include "Sound.h"
- // #include "Audio.h"
- extern "C" {
- #include <math.h>
- }
- typedef struct { float r; float i; } complex;
-
- // Object
- // To read in a sampled sound file
- // compute an estimate of the power Spectrum of a Delta t
- // write out the pse for each time interval
- // pse should be normalized to 0 to 1.0
- // Question should we plot log(power) or power???
-
-
- void NRICfft( float *, unsigned int, int);
-
-
-
- #define FILTERSIZE 16384
- #define FILTERSIZE2 32768
- #define LOGFILTERSIZE2 15
-
- #define PI2 6.283185307179586476925287
- int nfreqs, nintervals;
-
- #define MAXFREQS 256
- #define MAXINTERVALS 2000
- #define MAXSAMPS 8192
-
- complex tmpc[8192];
- void NRICfft(float *data,unsigned int nn,int isign);
- typedef signed short SAMPLE;
-
-
- float spectrum( SAMPLE * samples, int nsamples, float * ps, int nfreqs, float maxpwr )
- {
- int nfft;
- int i;
- SAMPLE * pb;
- float pwr,nfftsqd;
- complex * pc;
-
-
- for(pb = samples, pc = tmpc, i = 0; i < nsamples ; i+=2)
- { pc->r = (float) *pb++;
- pc->r += (float) *pb++;
- pc->i = 0.0; pc++;
- }
- nsamples >>= 1;
- nfft = 1;
- while (nfft < nsamples) nfft <<= 1;
- if (nfft > nsamples ) // zero fill
- for(i = 0; i < nfft-nsamples ; i++)
- { pc->r = 0.0; pc->i = 0.0 ; pc++; }
- pc = tmpc;
-
- NRICfft( (float *) pc,nfft,1);
- nfftsqd = float(nfft)*float(nfft);
-
- for(i=1; i<nfreqs; i++)
- {
-
- pwr = tmpc[i].r * tmpc[i].r;
- pwr += tmpc[i].i * tmpc[i].i;
- pwr = fsqrt(pwr/nfftsqd);
- pwr *= 1.0 + (( (float)(i)/(float)nfreqs)*.3);
- ps[i-1] = pwr;
- if (pwr > maxpwr) maxpwr = pwr;
- }
- return(maxpwr);
- }
-
-
-
-
-
- /* FFT from Numerical recipes in C page 411 */
- #define SWAP(a,b) tempr=(a); (a)=(b); (b) = tempr
-
- void NRICfft(float *data,unsigned int nn,int isign)
- {
- int istep,m,mmax,i,j;
- unsigned int n;
- double wtemp,wr,wpr,wpi,wi,theta;
- float tempr,tempi;
-
- data -= 1; ; /* Because the stupid routine is written for data[1...2*nn] */
- n = nn << 1;
- j=1;
- for(i=1;i<n;i+=2) {
- if( j>i) {
- SWAP(data[j],data[i]);
- SWAP(data[j+1],data[i+1]);
- }
- m = n >> 1;
- while (m >= 2 && j > m) {
- j -=m;
- m >>= 1;
- }
- j +=m;
- }
- mmax =2;
- while (n > mmax) {
- istep = 2*mmax;
- theta = PI2 / (isign*mmax);
- wtemp = sin(0.5*theta);
- wpr = -2.0*wtemp*wtemp;
- wpi = sin(theta);
- wr = 1.0;
- wi = 0.0;
- for ( m=1; m<mmax;m+=2) {
- for(i=m;i<n;i+=istep) {
- j = i+mmax;
- tempr = wr*data[j]-wi*data[j+1];
- tempi = wr*data[j+1]+wi*data[j];
- data[j] = data[i]-tempr;
- data[j+1] = data[i+1]-tempi;
- data[i] += tempr;
- data[i+1] += tempi;
- }
- wr = (wtemp=wr)*wpr-wi*wpi+wr;
- wi = wi*wpr+wtemp*wpi+wi;
- }
- mmax = istep;
- }
-
- }
-