Hej Kristian
Her kan du se hele koden
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class FrekvensData{
//:::::::Fields:::::::
static double[] x = new
double[AfstandRR.antalElementerIberegnArray]; // x-værdierne (tiden) fra
takogrammet
static double[] y = new
double[AfstandRR.antalElementerIberegnArray]; // y-værdierne
(antalSamples) fra takogrammet
static long nwk=0;
// størrelsen på de to arrrays lomb[] og frekvenser[]
static double[] frekvenser = new double[(int)nwk];
// array, der skal plottes som x-aksen i frekvensspektret
// består af værdier for det Lomb normaliseret periodogram
static double[] lomb = new double[(int)nwk];
// array, der skal plottes som y-aksen i frekvensspektret
// består af stigende frekvenser
static double ofac=0, hifac=0;
// ofac=oversamplings-faktor
//static double y=0, x=0;
// værdien, der extirpoleres ind i array-elementerne
static double[] yy = new double[(int)nwk];
// arrayet, der skal extirpoleres
static double mean=0.0;
static double var = 0.0;
// mean og varians
static double min=0.0, max=0.0;
// angiver henholdsvis 1. og sidste element i arrayet,
// der gives som input til meanvar()
static double nout=0.0;
// antal frekvenser i frekvenser[]
static boolean invFlag=false;
// Input til FFT
private static final double TOPI = 2.0 * Math.PI;
private static final int LOG2_MAXFFTSIZE = 15;
// Begrænsning på antal samples der kan bruges
private static final int MAXFFTSIZE = 1 << LOG2_MAXFFTSIZE;
static int [] bitreverse = new int[MAXFFTSIZE];
static int bits=9;
static int antalElementerIberegnArray =
AfstandRR.antalElementerIberegnArray;
//::::::::Metoder::::::::::
public static void spektralanalyse(double[] x, double[] y,
int n, double ofac, double hifac, double[] frekvenser,
double[] lomb, long nwk, double nout){
long ndim, nfreq, nfreqt;
double hs2wt, hypo, pmax, sterm, swt, var, xdif, xmax, xmin;
double ck, ckk, cterm, cwt, den, df, effm, expy, fndim, fac, hc2wt;
nout = 0.5*(long)ofac*(long)hifac*n;
nfreqt = (long)ofac*(long)hifac*4; // 4
refererer til antallet af interpolationspunkter
// per 1/4 cyklus
af den højeste frekvens.
nfreq = 64; // angiver
størrelsen af FFT som den næste potens af 2
// over nfreqt.
while(nfreq < nfreqt){
nfreq = nfreq << 1; // skifter bits for
nfreq 1 til venstre
}
ndim = nfreq << 1;
if(ndim > nwk){
System.out.println("Workspace er for lille!");
}
meanvar(y,n); // beregner mean,
varians og data-range
if(var==0.0){
System.out.println("Der er ingen varians i data!");
}
xmin = x[1];
xmax = xmin;
for(int j=2; j<=n; j++){
if(x[j] < xmin) xmin = x[j];
if(x[j] > xmin) xmax = x[j];
}
xdif = xmax-xmin;
for(int j=1; j<=ndim; j++){
frekvenser[j]=lomb[j]=0.0; //
nulstiller workspace
}
fac = ndim/(xdif*ofac);
fndim = ndim;
for(int j=1; j<=n; j++){ // extirpolerer
data ind i workspace
ck = (x[j]-xmin)*fac;
while(ck >= fndim) ck=ck-fndim;
ckk = 2.0*(ck++);
while(ckk >= fndim) ckk=ckk-fndim;
++ckk;
extirpolation(y[j]-mean, frekvenser, ndim, ck, 4);
extirpolation(1.0, lomb, ndim, ckk, 4);
}
double [] nullArray = new double[512];
// Laver et array fyldt med nuller
for(int j=0; j<=511; j++){
nullArray[j]=0;
}
// + noget kode, der sørger for at frekvenser og lomb består af
512 elementer
FFT(9);
doFFT(frekvenser, nullArray, invFlag);
// tager Fast Fourier Transformation af data i frekvenser[]
doFFT(lomb, nullArray, invFlag);
// tager Fast Fourier Transformation af data i lomb[]
df = 1.0/(xdif*ofac);
pmax = -1.0;
for(int k=3,j=1;j<nout;j++,k+=2){ // beregner
Lomb-værdien for hver frekvens
hypo = Math.sqrt(lomb[k]*lomb[k]+lomb[k+1]*lomb[k+1]);
hc2wt = 0.5*lomb[k]/hypo;
hs2wt = 0.5*lomb[k+1]/hypo;
cwt = Math.sqrt(0.5+hc2wt);
//****
if(hs2wt<0){
swt = -Math.abs(Math.sqrt(0.5-hc2wt));
}
else{
swt = Math.abs(Math.sqrt(0.5-hc2wt));
}
//************
den = 0.5*n+hc2wt*lomb[k]+hs2wt*lomb[k+1];
cterm = Math.pow(cwt*frekvenser[k]+swt*frekvenser[k+1],2)/den;
sterm =
Math.pow(cwt*frekvenser[k+1]-swt*frekvenser[k],2)/(n-den);
frekvenser[j] = j*df;
lomb[j] = (cterm+sterm)/(2.0*var);
// if(lomb[j] > pmax) pmax = lomb[jmax=j]; // (kan måske
undvære?!)
}
}
/**
* FFT class constructor
* Initializes code for doing a fast Fourier transform
*
* @param int bits is a power of two such that 2^b is the number
* of samples.
*/
public static void FFT(int bits) {
if (bits > LOG2_MAXFFTSIZE) {
System.out.println("" + bits + " For lang");
System.exit(1);
}
for (int i = (1 << bits) - 1; i >= 0; --i) {
int k = 0;
for (int j = 0; j < bits; ++j) {
k *= 2;
if ((i & (1 << j)) != 0)
k++;
}
bitreverse[i] = k;
}
}
/**
* A fast Fourier transform routine
*
* @param double [] xr is the real part of the data to be
transformed
* @param double [] xi is the imaginary part of the data to be
transformed
* (normally zero unless inverse transofrm is effect).
* @param boolean invFlag which is true if inverse transform is
being
* applied. false for a forward transform.
*/
public static void doFFT(double [] xr, double [] xi, boolean
invFlag) {
int n, n2, i, k, kn2, p;
double ang, s, c, tr, ti;
n2 = (n = (1 << bits)) / 2;
for (int l = 0; l < bits; ++l) {
for (k = 0; k < n; k += n2) {
for (i = 0; i < n2; ++i, ++k) {
p = bitreverse[k / n2];
ang = TOPI * p / n;
c = Math.cos(ang);
s = Math.sin(ang);
kn2 = k + n2;
if (invFlag)
s = -s;
tr = xr[kn2] * c + xi[kn2] * s;
ti = xi[kn2] * c - xr[kn2] * s;
xr[kn2] = xr[k] - tr;
xi[kn2] = xi[k] - ti;
xr[k] += tr;
xi[k] += ti;
}
}
n2 /= 2;
}
for (k = 0; k < n; k++) {
if ((i = bitreverse[k]) <= k)
continue;
tr = xr[k];
ti = xi[k];
xr[k] = xr[i];
xi[k] = xi[i];
xr[i] = tr;
xi[i] = ti;
}
// Finally, multiply each value by 1/n, if this is the forward
// transform.
if (!invFlag) {
double f = 1.0 / n;
for (i = 0; i < n; i++) {
xr[i] *= f;
xi[i] *= f;
}
}
}
public static void extirpolation(double y, double[] yy, long nwk,
double x, int m) {
int ihi=0, ilo=0, ix=0, nden=0;
int[] nfac = {0, 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
//coefficienter fra Lagrange interpolations polynomiet
double fac;
if (m > 10) {
System.out.println("Fejl 40!
");
return;
}
ix = (int)x;
if(x == (double)ix){
yy[ix] = yy[ix] + y;
System.out.println("if-løkke");
}
else {
int max = 0;
if ((long) (x - 0.5 * m + 1.0) > 1) {
max = (int) (x - 0.5 * m + 1.0);
}
else {
max = 1;
}
if (max < nwk - m + 1) {
ilo = max;
}
else {
ilo = (int) nwk - m + 1;
}
ihi = ilo + m - 1;
nden = nfac[m];
fac = x - ilo;
for (int j = ilo + 1; j <= ihi; j++) {
fac = fac * (x - j);
}
yy[ihi] = yy[ihi] + y * fac / (nden * (x - ihi));
for (int j = ihi - 1; j >= ilo; j--) {
nden = (nden / (j + 1 - ilo)) * (j - ihi);
yy[j] = yy[j] + y * fac / (nden * (x - j));
}
//System.out.println("else-løkke");
}
}
public static double meanvar(double[] data, int n) { // metode,
der beregner mean, varians og data-range
double element= 0, sum=0, squareSum=0;
for (int i = 0; i <= (n-1); i++) {
element = data[i];
sum = sum + element;
}
mean = sum / n;
for (int i = 0; i <= (n-1); i++) {
element = data[i];
element = Math.pow(element,2);
squareSum = squareSum + element;
}
var = ((n * squareSum) - Math.pow(sum,2)) / Math.pow(n,2);
min = data[0];
// henter 1. element i data[]
max = data[n-1];
return var;
// henter sidste element i data[]
//System.out.println("Mean: "+ mean+ " Varians: "+ var);
}
}
Kristian Thy wrote:
> Christian Gammelgaard wrote:
>
>> Jeg har et lille problem, da min compiler (JCreator) melder følgende
>> fejl:
>
>
> Strengt taget er JCreator dit IDE. Din compiler er stadig javac.
>
>> FrekvensData.java:61: variable var might not have been initialized
>> if(var==0.0){
>> ^
>>
>> men variablen "var" ER deklareret på følgende måde, i toppen af klassen:
>>
>> static double var = 0.0;
>>
>> Hvad kan dette skyldes?
>
>
> Du har muligvis ikke var i samme scope i linie 61 som der hvor du
> initialiserer den. Det er svært at sige uden at se koden.
>
> \\kristian