📄 fouriertransform.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.preprocessing;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleTable;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.example.SimpleExampleSet;
import edu.udo.cs.yale.example.DataRowReader;
import edu.udo.cs.yale.example.DataRow;
import edu.udo.cs.yale.tools.math.*;
import edu.udo.cs.yale.tools.Ontology;
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
/** Creates a new example set consisting of the result of a fourier transformation for each attribute
* of the input example set.
*
* @version $Id: FourierTransform.java,v 1.3 2004/09/09 12:00:53 ingomierswa Exp $
*/
public class FourierTransform extends Operator {
private static final Class[] INPUT_CLASSES = { ExampleSet.class };
private static final Class[] OUTPUT_CLASSES = { ExampleSet.class };
public IOObject[] apply() throws OperatorException {
// init
ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);
List attributes = new LinkedList();
// create new example table
int numberOfNewExamples = FastFourierTransform.getGreatestPowerOf2LessThan(exampleSet.getSize()) / 2;
ExampleTable exampleTable = new MemoryExampleTable(new LinkedList(), numberOfNewExamples);
// create id attribute (for frequency)
Attribute idAttribute = new Attribute("Id", Ontology.INTEGER, Ontology.SINGLE_VALUE,
Attribute.UNDEFINED_BLOCK_NR, null);
exampleTable.addAttribute(idAttribute);
//attributes.add(idAttribute);
DataRowReader drr = exampleTable.getDataReader();
int k = 0;
while (drr.hasNext()) {
DataRow dataRow = drr.next();
dataRow.set(idAttribute, FastFourierTransform.convertFrequency(k++, numberOfNewExamples, exampleSet.getSize()));
}
// create fft values
double totalMaxEvidence = Double.NEGATIVE_INFINITY;
Attribute label = exampleSet.getLabel();
FastFourierTransform fft = new FastFourierTransform(WindowFunction.BLACKMAN_HARRIS);
SpectrumFilter filter = new SpectrumFilter(SpectrumFilter.NONE);
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
Attribute current = exampleSet.getAttribute(i);
if (!current.isNominal()) {
Complex[] result = fft.getFourierTransform(exampleSet, label, current);
Peak[] spectrum = filter.filter(result, exampleSet.getSize());
// create new attribute and fill table with values
Attribute newAttribute = new Attribute("fft(" + current.getName() + ")",
Ontology.REAL, Ontology.SINGLE_VALUE,
Attribute.UNDEFINED_BLOCK_NR, null);
exampleTable.addAttribute(newAttribute);
attributes.add(newAttribute);
fillTable(exampleTable, newAttribute, spectrum);
}
}
ExampleSet resultSet = new SimpleExampleSet(exampleTable, attributes);
resultSet.setIdAttribute(idAttribute);
resultSet.recalculateAllAttributeStatistics();
return new IOObject[] { resultSet };
}
/** Fills the table with the length of the given complex numbers in the column of the attribute. */
private void fillTable(ExampleTable table, Attribute attribute, Peak[] values) {
DataRowReader reader = table.getDataReader();
int k = 0;
while (reader.hasNext()) {
DataRow dataRow = reader.next();
dataRow.set(attribute, values[k++].getMagnitude());
}
}
public Class[] getInputClasses() { return INPUT_CLASSES; }
public Class[] getOutputClasses() { return OUTPUT_CLASSES; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -