傅立叶变换得到大约一半的输出错误
Posted
技术标签:
【中文标题】傅立叶变换得到大约一半的输出错误【英文标题】:Fourier Transform get about half the outputs wrong 【发布时间】:2014-03-31 01:49:42 【问题描述】:我的项目播放音乐并不断将贝司的“强度”写入文本文件。然而,大约一半的强度是错误的,这意味着我的傅立叶变换输出被加速了。它会产生这样的输出:http://pastebin.com/yxyBwv2Q 所有的条都应该排成一行。该 pastebin 中有 3 种不同的音调,按 (3,1,2) 的顺序排列它们的敲击力度。这是有道理的,该项目肯定会显示何时且仅当低音重击时。它根本不平滑,有一个误差范围。
这是代码:
package script;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.DecoderException;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.Obuffer;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.player.Player;
import java.text.NumberFormat;
import java.util.Arrays;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.Audiosystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.tritonus.share.sampled.file.TAudioFileFormat;
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
public class MusicPlayer
static double getDurationWithMp3Spi(File file)
throws UnsupportedAudioFileException, IOException
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
if (fileFormat instanceof TAudioFileFormat)
Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
String key = "duration";
Long microseconds = (Long) properties.get(key);
int mili = (int) (microseconds / 1000);
int sec = (mili / 1000);
int min = (mili / 1000) / 60;
return microseconds / 1000000.0;
else
throw new UnsupportedAudioFileException();
public static void main(String[] args) throws IOException,
JavaLayerException, InterruptedException, UnsupportedAudioFileException, AWTException
// TODO Auto-generated method stub
//E:\\Program Files (x86)\\Steam\\SteamApps\\common\\team fortress 2\\tf\\cfg\\script.cfg
FileInputStream mp3 = new FileInputStream("30hz.mp3");
FileInputStream mp3player = new FileInputStream("30hz.mp3");
File song = new File("30hz.mp3");
Decoder decoder = new Decoder();
Bitstream bitstream = new Bitstream(mp3);
Bitstream playerBitstream = new Bitstream(mp3player);
SampleBuffer currentBuffer = (SampleBuffer) decoder.decodeFrame(
bitstream.readFrame(), bitstream);
Player player = new Player(mp3player);
int bufferLength = currentBuffer.getBufferLength();
DoubleFFT_1D transform = new DoubleFFT_1D(bufferLength);
double duration = getDurationWithMp3Spi(song);
bitstream.unreadFrame();
bitstream.closeFrame();
String fov;
int x =0;
int placeholder=0;
double currentMaximum=Double.MIN_VALUE;
double maximum=20000;
int freq=0;
Robot robot = new Robot();
String text=" ";
String[] textAmp = new String[200];
for(int i=0;i<200;i++)
textAmp[i]="O";
for(int j=0;j<i;j++)
textAmp[i]=textAmp[i]+"O";
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
player.play(1);
double intensity=0;
while (((x <= (int) (((44100 * (duration - 1)) / 1152.0)))))
player.play(1);
currentBuffer = (SampleBuffer) decoder.decodeFrame(bitstream.readFrame(), bitstream);
short[] originalBufferArray = new short[bufferLength];
originalBufferArray = currentBuffer.getBuffer();
double[] doubleBufferArray = new double[bufferLength*2];
for(int i =0; i<bufferLength;i++)
doubleBufferArray[i]=(double)originalBufferArray[i];
double temp=0;
//BEGIN ARRAYLIST
// double[] organizedArray = new double[2304];
// for(int i=0;i<1152;i++)
// organizedArray[i*2]=doubleBufferArray[i];
//
// for(int i=1152;i<2304;i++)
// organizedArray[((i-1152)*2)+1]=doubleBufferArray[i];
//
//END ARRAYLIST
transform.complexForward(doubleBufferArray,0);
double[] finalArray = new double[bufferLength];
for(int i =0;i<10;i+=2)
finalArray[i/2]= Math.sqrt(Math.pow(doubleBufferArray[i],2)+(Math.pow(doubleBufferArray[i+1], 2)));
finalArray[i/2]= finalArray[i/2]/(double)(bufferLength/2.0);
if(finalArray[i/2]>currentMaximum)
currentMaximum=finalArray[i/2];
freq=i/2;
// if (freq==4)
// currentMaximum/=4;
// intensity=currentMaximum/maximum;
//
//
// else if (freq==3)
// currentMaximum/=3;
// intensity=currentMaximum/maximum;
//
// else if (freq==2)
// currentMaximum/=2;
// intensity=currentMaximum/maximum;
//
// else
intensity=currentMaximum/maximum;
int fovInt=(int)(90-(intensity*25));
//
// text=String.valueOf(intensity);
// text=text.substring(2,4);
// placeholder = (int)Double.parseDouble(text);
try
File script = new File("E:\\Program Files (x86)\\Steam\\SteamApps\\common\\team fortress 2\\tf\\cfg\\script.cfg");
BufferedWriter writer = new BufferedWriter(new FileWriter(script));
fov = "fov_desired " + fovInt +"\necho \"working\"";
writer.write(fov, 0 ,fov.length());
writer.flush();
writer.close();
robot.keyPress(KeyEvent.VK_NUM_LOCK );
robot.keyRelease(KeyEvent.VK_NUM_LOCK );
finally
currentMaximum=Double.MIN_VALUE;
bitstream.unreadFrame();
bitstream.closeFrame();
x++;
/*
* 0~0hz
* 1~19.140625hz
* 2~38.28125hz
* 3~57.422145hz
* 4~76.56277
* 5~ 95.703395
*/
有人知道为什么我的傅立叶分析会得到奇怪的结果吗?开始时的播放也是如此,歌曲与输出同步。
【问题讨论】:
【参考方案1】:对于与波形不同步的短 FFT,幅度会出现一些扇形,具体取决于输入信号相对于窗口边缘的相位。使 FFT 更长,可能是低频长度的多个周期,将最大限度地减少这种情况,就像使用非矩形窗口(Von Hann 等)一样。对于 40 Hz,您可以尝试长度至少为 8192 个样本(大约 8 个 43 Hz 周期)或更长的 FFT。
【讨论】:
嗯,好的,那我试试。是否有某个阈值,例如 8192,可以让所有低频完全正确?还是我应该只取最大值 44100?我想我越慢我的程序就会越慢,至于 44100 它一次只能处理一秒钟的数据。我很困惑,有没有办法在一首歌中快速获得所有 21050 频率?我似乎只能获得 1152 个频率。 没有所有频率的确切数量。例如,某些声音的频谱分量可能为 261.63 Hz。 FFT 越长,得到的频率箱越多,分辨率也越高。以上是关于傅立叶变换得到大约一半的输出错误的主要内容,如果未能解决你的问题,请参考以下文章