001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.demos.sandbox.audio; 034 035import java.awt.BorderLayout; 036import java.awt.Dimension; 037import java.awt.GridBagConstraints; 038import java.awt.GridBagLayout; 039import java.util.Arrays; 040import java.util.List; 041 042import javax.swing.JFrame; 043import javax.swing.JPanel; 044 045import org.jfree.chart.ChartFactory; 046import org.jfree.chart.ChartPanel; 047import org.jfree.chart.JFreeChart; 048import org.jfree.chart.plot.PlotOrientation; 049import org.jfree.data.xy.DefaultXYDataset; 050import org.openimaj.audio.filters.MelFilterBank; 051import org.openimaj.audio.filters.TriangularFilter; 052 053/** 054 * Displays Mel Filters from a Mel Filter filter bank 055 * 056 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 057 * @created 26 Jul 2012 058 * @version $Author$, $Revision$, $Date$ 059 */ 060public class MelFilters extends JPanel 061{ 062 /** */ 063 private static final long serialVersionUID = 1L; 064 065 /** 066 * Default constructor 067 */ 068 public MelFilters() 069 { 070 init(); 071 } 072 073 /** 074 * Initialise the chart 075 */ 076 private void init() 077 { 078 final MelFilterBank mfb = new MelFilterBank(40, 300, 3000); 079 final List<TriangularFilter> filters = mfb.getFilters(); 080 081 System.out.println(filters.size() + " filters"); 082 083 DefaultXYDataset xy = new DefaultXYDataset(); 084 int i = 0; 085 for (final TriangularFilter f : filters) 086 { 087 final double[][] data = new double[2][3]; 088 data[1][0] = f.getLowFrequency(); 089 data[0][0] = 0; 090 data[1][1] = f.getCentreFrequency(); 091 data[0][1] = f.getFilterAmplitude(); 092 data[1][2] = f.getHighFrequency(); 093 data[0][2] = 0; 094 xy.addSeries("Filter " + i, data); 095 i++; 096 } 097 098 System.out.println(i); 099 100 JFreeChart c = ChartFactory.createXYLineChart("Mel Filter Responses", "Amplitude", 101 "Frequency", xy, PlotOrientation.HORIZONTAL, false, false, 102 false); 103 ChartPanel chartPanel = new ChartPanel(c, false); 104 chartPanel.setPreferredSize(new Dimension(1500, 300)); 105 106 this.setLayout(new GridBagLayout()); 107 final GridBagConstraints gbc = new GridBagConstraints(); 108 gbc.gridx = gbc.gridy = 0; 109 gbc.weightx = gbc.weighty = 1; 110 gbc.fill = GridBagConstraints.BOTH; 111 this.add(chartPanel, gbc); 112 113 xy = new DefaultXYDataset(); 114 i = 0; 115 final int nSamples = 1024; 116 for (final TriangularFilter f : filters) 117 { 118 final double[] response = f.getResponseCurve(nSamples, 4000); 119 System.out.println("Filter " + i + ": " + Arrays.toString(response)); 120 final double[][] xxyy = new double[2][response.length]; 121 for (int x = 0; x < response.length; x++) 122 { 123 xxyy[1][x] = x * (4000 / nSamples); 124 xxyy[0][x] = response[x]; 125 } 126 xy.addSeries("Filter " + i, xxyy); 127 i++; 128 } 129 130 c = ChartFactory.createXYLineChart("Mel Filter Calculated Responses", "Amplitude", 131 "Frequency", xy, PlotOrientation.HORIZONTAL, false, false, 132 false); 133 chartPanel = new ChartPanel(c, false); 134 chartPanel.setPreferredSize(new Dimension(1500, 300)); 135 gbc.gridy++; 136 this.add(chartPanel, gbc); 137 } 138 139 /** 140 * 141 * @param args 142 */ 143 public static void main(String[] args) 144 { 145 final JFrame f = new JFrame(); 146 f.getContentPane().add(new MelFilters(), BorderLayout.CENTER); 147 f.pack(); 148 f.setVisible(true); 149 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 150 } 151}