傅里叶变换得到大约一半的输出错误

时间:2014-03-31 01:49:43

标签: java arrays audio fft

我的项目播放音乐并不断将低音的“强度”写入文本文件。然而,大约一半的强度是错误的,这意味着我的傅里叶变换输出被猛拉或者其他东西。它会产生如下输出: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 个答案:

答案 0 :(得分:0)

对于与波形不同步的短FFT,根据输入信号相对于窗口边缘的相位,会有一些幅度的扇形。如果使用非矩形窗口(Von Hann等),使FFT更长,可能是您的低频长度的多个周期将最小化这一点。对于40 Hz,您可以尝试使用至少8192个样本(大约8个43 Hz周期)或更长的FFT。