java - 傅立叶变换得到大约一半错误输出

标签 java arrays audio fft

我的项目演奏音乐,并不断将低音的“强度”写入文本文件。但是,大约一半的强度是错误的,这意味着我的傅立叶变换输出被弄坏了。它会导致这样的输出:http://pastebin.com/yxyBwv2Q所有条形图应对齐的位置。该糊盒中有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
 */




    }
        }

有人知道为什么我的傅里叶分析使我的结果怪异吗?开头的播放也是这样,因此歌曲与输出同步。

最佳答案

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

关于java - 傅立叶变换得到大约一半错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752433/

相关文章:

java - 在 JUnit Test 子类中引用 protected 变量时获取 NullPointerException

java - Android - 如何强制 child 覆盖具有代码的父方法

c - 如何在 C 函数中返回超过 1 个值?

c - 从 void 双指针数组输出 void 指针

iphone - iPhone 上的音频合成从哪里开始

将 Swift 闭包转换为 CFunctionPointer?

android - Android,libgdx,声音问题

java - 如何在Java中为自定义堆栈类编写打印函数?

java - 不同级别logback不同文件

php - 使用事件记录形成查询