在我正在读高中的论文范围内,我选择从头制作自己的音频文件到频谱图转换器,以便根据这些频谱图创建景观。

我已经实现了FFT并使用它来制作高度图(频谱图)。但是,当频率变得密集时,我经常会得到垂直条纹形式的怪异伪像,如下图所示。

java - 如何消除光谱图中的垂直条纹?-LMLPHP

该示例恰好在开头,窗口长度为2048,对数为2。我正在使用的FFT是完美无缺的,我已经将其与其他FFT进行了比较,它们产生的结果相同。

这是将振幅转换为频率并将其存储在2D数组中的功能:

private void transform(int from, int until) {
    double val, step;
    for(int i=from; i<until; i++) {
        for(int j=0; j<n; j++)
            chunk[j] = data[0][i*n+j+start];

        fft.realForward(chunk);

        for(int j=0; j<height; j++) {
            val = Math.sqrt(chunk[2*j]*chunk[2*j] + chunk[2*j+1]*chunk[2*j+1]);
            map[i][j] = val;
        }
    }
}

现在我的问题是:这些垂直条纹从哪里来,我如何摆脱它们?

我目前不使用窗口函数,并且每个计算都相互串接,这意味着没有重叠。这是您想到制作频谱图的最简单方法。它可以帮助引入窗口函数还是独立于框架是否已参与先前的计算而进行每次计算,也就是说与框架窗口重叠吗?

另外,还有什么其他方法可以改善我的基本方法以获得更好的结果?

这是全类。我从音频文件中馈送数据和所有必要的信息:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import org.jtransforms.fft.DoubleFFT_1D;

public class Heightmap extends JFrame implements WindowListener{
    public static final int LOG_SCALE = 0;
    public static final int LOG_SQUARE_SCALE = 1;
    public static final int SQUARE_SCALE = 2;
    public static final int LINEAR_SCALE = 3;

    private BufferedImage heightmap;
    private FileDialog chooser;

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem save, close;

    private DoubleFFT_1D fft;
    private int[][] data;
    private double[][] map;
    private double[] chunk;
    private int width, height, n, start, scale;
    private String name;

    private boolean inactive;

    public Heightmap(int[][] data, int resolution, int start,
            int width, int height, int scale, String name) {

        this.data = data;
        this.n = resolution;
        this.start = start;
        this.width = width;
        this.height = height;
        this.scale = scale;
        this.name = name;

        fft = new DoubleFFT_1D(n);
        map = new double[width][height];
        heightmap = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        chunk = new double[n];

        System.out.println("Starting transformation...");

        long time;
        time = System.currentTimeMillis();
        transform();
        time = System.currentTimeMillis() - time;
        System.out.println("Time taken for calculation: "+time+" ms");

        time = System.currentTimeMillis();
        makeHeightmap();
        initComponents();
        time = System.currentTimeMillis() - time;
        System.out.println("Time taken for drawing heightmap: "+time+" ms");

    }

    private void initComponents() {
        this.setSize(width, height);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setTitle(name);

        createMenuBar();
        chooser = new FileDialog(this, "Choose a directory", FileDialog.SAVE);
        chooser.setDirectory("/Users/<user>/Desktop");

        this.addMouseListener(new HeightmapMouseListener());
        this.addKeyListener(new HeightmapKeyListener());
        this.addWindowListener(this);

        this.setVisible(true);

    }

    private void createMenuBar() {
        menuBar = new JMenuBar();
        fileMenu = new JMenu();

        fileMenu.setText("File");

        save = new JMenuItem("Save...", KeyEvent.VK_S);
        save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.META_DOWN_MASK));
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                chooser.setVisible(true);

                String fileName = chooser.getFile();
                String dir = chooser.getDirectory();
                chooser.setDirectory(dir);

                if(fileName != null) {
                    try {
                        File outputfile = new File(dir + fileName + ".png");
                        ImageIO.write(heightmap, "png", outputfile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Saved "+fileName+".png to "+dir);
                }
            }
        });

        close = new JMenuItem("Close", KeyEvent.VK_C);
        close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_DOWN_MASK));
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });

        fileMenu.add(save);
        fileMenu.addSeparator();
        fileMenu.add(close);

        menuBar.add(fileMenu);
        this.setJMenuBar(menuBar);
    }

    public void paint(Graphics g) {
        g.drawImage(heightmap, 0, 0, null);
    }

    private void transform() {
        transform(0, width);
    }
    private void transform(int from, int until) {
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        double val, step;
        for(int i=from; i<until; i++) {
            for(int j=0; j<n; j++) {
                chunk[j] = data[0][i*n+j+start];
            }
            fft.realForward(chunk);

            for(int j=0; j<height; j++) {
                val = Math.sqrt(chunk[2*j]*chunk[2*j] + chunk[2*j+1]*chunk[2*j+1]);

                if(val > max)
                    max = val;
                if(val < min)
                    min = val;
                map[i][j] = val;
            }

            if(min != 0) {
                step = max/(max-min);
                for(int j=0; j<height; j++)
                    map[i][j] = (map[i][j]-min)*step;
            }
        }
    }

    /*
     * Paints heightmap into the BufferedImage
     */
    private void makeHeightmap() {
        double max = 0;
        switch(scale) {
        case LOG_SCALE: max = Math.log(findMax(map)+1); break;
        case LOG_SQUARE_SCALE: max = Math.pow(Math.log(findMax(map)+1), 2); break;
        case SQUARE_SCALE: max = Math.sqrt(findMax(map)); break;
        case LINEAR_SCALE: max = findMax(map); break;
        default: max = Math.pow(Math.log(findMax(map)+1), 2); break;
        }
        double stepsize = 255.0/max;
        int val, rgb;

        for(int x=0; x<width; x++)
            for(int y=0; y<height; y++) {
                switch(scale) {
                case LOG_SCALE: val = (int) (Math.log(map[x][y]+1)*stepsize); break;
                case LOG_SQUARE_SCALE: val = (int) (Math.log(map[x][y]+1)*stepsize); val *= val; break;
                case SQUARE_SCALE: val = (int) (Math.sqrt(map[x][y])*stepsize); break;
                case LINEAR_SCALE: val = (int) (map[x][y]*stepsize); break;
                default: val = (int) (Math.log(map[x][y]+1)*stepsize); val *= val; break;
                }
                rgb = 255<<24 | val<<16 | val<<8 | val;
                heightmap.setRGB(x, height-y-1, rgb);
            }

    }

    private double findMax(double[][] data) {
        double max = 0;
        for(double[] val1: data)
            for(double d: val1)
                if(d > max)
                    max = d;
        return max;
    }

    private class HeightmapKeyListener implements KeyListener {
        boolean busy = false;

        public void keyPressed(KeyEvent e) {

            if(e.getKeyCode() == KeyEvent.VK_RIGHT && !busy && start < data[0].length-width*n) {
                busy = true;

                for(int x=0; x<width-1; x++)
                    map[x] = map[x+1].clone();

                start += n;
                transform(width-1, width);
                makeHeightmap();
                repaint();
                busy = false;
            }
            else if(e.getKeyCode() == KeyEvent.VK_LEFT && !busy && start > 0) {
                busy = true;

                for(int x=width-1; x>0; x--)
                    map[x] = map[x-1];

                start -= n;
                transform(0, 1);
                makeHeightmap();
                repaint();
                busy = false;
            }
        }

        public void keyReleased(KeyEvent e) {   }
        public void keyTyped(KeyEvent e) {  }
    }

    private class HeightmapMouseListener implements MouseListener {


        public void mouseClicked(MouseEvent e) {
            if(inactive) {
                inactive = false;
                return;
            }

            long time = System.currentTimeMillis();

            int posX = e.getX();
            int diff = posX - width/2;  //difference between old and new center in pixels
            int oldStart = start;

            start = start + diff*n;
            if(start < 0) start = 0;
            int maxFrame = data[0].length-width*n;
            if(start > maxFrame) start = maxFrame;
            if(start == oldStart) return;

            System.out.println("Changing center...");

            int absDiff = Math.abs(diff);
            if(start < oldStart) {  //shift the start backward, recalculate the start
                for(int x=width-1; x>=absDiff; x--)
                    map[x] = map[x-absDiff].clone();
                transform(0, absDiff);
            }
            else if(start > oldStart) { //shift the back forward, recalculate the back
                for(int x=0; x<width-absDiff; x++)
                    map[x] = map[x+absDiff].clone();
                transform(width-absDiff, width);
            }

            makeHeightmap();
            repaint();
            System.out.println("Time taken: "+(System.currentTimeMillis()-time)+" ms");

        }

        public void mousePressed(MouseEvent e) {    }
        public void mouseReleased(MouseEvent e) {   }
        public void mouseEntered(MouseEvent e) {    }
        public void mouseExited(MouseEvent e) { }
    }

    public void windowActivated(WindowEvent arg0) { }
    public void windowClosed(WindowEvent arg0) {    }
    public void windowClosing(WindowEvent arg0) {   }
    public void windowDeactivated(WindowEvent arg0) {
        inactive = true;
    }
    public void windowDeiconified(WindowEvent arg0) {   }
    public void windowIconified(WindowEvent arg0) { }
    public void windowOpened(WindowEvent arg0) {    }

}

编辑:
实现窗口功能可显着改善结果。我真的不明白窗口函数会做什么,因此低估了它的作用。

但是,这样做之后,我尝试映射频率为10kHz的余弦波,这再次产生了一些奇怪的伪像:

这可能是什么原因?我通过将所有内容从0剪切到0到25​​5剪切到255来实现溢出保护,没有任何变化。

最佳答案

这种伪影的原因可能是由于颜色映射函数之前或之中的溢出或超出了参数范围,或者可能是某些函数(对数)返回了NaN值。您可以通过为超出范围的值或非法值添加一些断言来找到此错误。

关于java - 如何消除光谱图中的垂直条纹?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33466838/

10-12 02:40