- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在为 Google Glass 开发一个应用程序,该应用程序自录制音频以来实时显示峰值电流峰值频率。我当前的问题是频率报告变化非常快,因此很难确定频率我也不确定我的 NumberFormat 输出格式是否正确,因为它只达到“00.000”。我可能需要一些窗口方面的帮助,但我对它的理解就在那里。
谢谢!
public class RTAactivity extends Activity {
private static final int SAMPLING_RATE = 44100;
private TextView tvfreq;
private TextView tvdb;
private RecordingThread mRecordingThread;
private int mBufferSize;
private short[] mAudioBuffer;
private String mDecibelFormat;
private double mFreqFormat = 0.0;
private int blockSize = 1024; //4096
private DoubleFFT_1D fft;
private int[] bufferDouble, bufferDouble2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rta_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
tvfreq = (TextView) findViewById(R.id.tv_freq);
tvdb = (TextView) findViewById(R.id.tv_decibels);
// Compute the minimum required audio buffer size and allocate the buffer.
mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mAudioBuffer = new short[mBufferSize / 2];
bufferDouble2 = new int[mBufferSize /2];
bufferDouble = new int[(blockSize-1) * 2 ];
mDecibelFormat = getResources().getString(R.string.decibel_format);
}
@Override
protected void onResume() {
super.onResume();
mRecordingThread = new RecordingThread();
mRecordingThread.start();
}
@Override
protected void onPause() {
super.onPause();
if (mRecordingThread != null) {
mRecordingThread.stopRunning();
mRecordingThread = null;
}
}
private class RecordingThread extends Thread{
private boolean mShallContinue = true;
@Override
public void run() {
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
AudioRecord record = new AudioRecord(AudioSource.MIC, SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize);
short[] buffer = new short[blockSize];
double[] audioDataDoubles = new double[(blockSize * 2)];
double[] re = new double[blockSize];
double[] im = new double[blockSize];
double[] magnitude = new double[blockSize];
//start collecting data
record.startRecording();
DoubleFFT_1D fft = new DoubleFFT_1D(blockSize);
while (shallContinue()) {
/**decibels */
record.read(mAudioBuffer, 0, mBufferSize / 2);
updateDecibelLevel();
/**frequency */
///windowing!?
for(int i=0;i<mAudioBuffer.length;i++) {
bufferDouble2[i] = (int) mAudioBuffer[i];
}
for(int i=0;i<blockSize-1;i++){
double x=-Math.PI+2*i*(Math.PI/blockSize);
double winValue=(1+Math.cos(x))/2.0;
bufferDouble[i]= (int) (bufferDouble2[i]*winValue); }
// bufferDouble[2*i]=bufferDouble2[i];
// bufferDouble[2*i+1] = (int) 0.0;}
int bufferReadResult = record.read(buffer, 0, blockSize);
// Read in the data from the mic to the array
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
audioDataDoubles[2 * i] = (double) buffer[i] / 32768.0; // signed 16 bit
audioDataDoubles[(2 * i) + 1] = 0.0;
}
//audiodataDoubles now holds data to work with
fft.complexForward(audioDataDoubles); //complexForward
// Calculate the Real and imaginary and Magnitude.
for (int i = 0; i < blockSize; i++) {
double real = audioDataDoubles[2 * i];
double imag = audioDataDoubles[2 * i + 1];
magnitude[i] = Math.sqrt((real * real) + (imag * imag));
}
for (int i = 0; i < blockSize; i++) {
// real is stored in first part of array
re[i] = audioDataDoubles[i * 2];
// imaginary is stored in the sequential part
im[i] = audioDataDoubles[(i * 2) + 1];
// magnitude is calculated by the square root of (imaginary^2 + real^2)
magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i] * im[i]));
}
double peak = -1.0;
// Get the largest magnitude peak
for (int i = 0; i < blockSize; i++) {
peak = magnitude[i];
}
// calculated the frequency
mFreqFormat = (SAMPLING_RATE * peak) / blockSize;
updateFrequency();
}
record.stop(); //stop recording please.
record.release(); // Deystroy the recording, PLEASE!
}
/**true if the thread should continue running or false if it should stop
*/
private synchronized boolean shallContinue() {return mShallContinue; }
/** Notifies the thread that it should stop running at the next opportunity. */
private synchronized void stopRunning() { mShallContinue = false; }
private void updateDecibelLevel() {
// Compute the root-mean-squared of the sound buffer and then apply the formula for
// computing the decibel level, 20 * log_10(rms). This is an uncalibrated calculation
// that assumes no noise in the samples; with 16-bit recording, it can range from
// -90 dB to 0 dB.
double sum = 0;
for (short rawSample : mAudioBuffer) {
double sample = rawSample / 32768.0;
sum += sample * sample;
}
double rms = Math.sqrt(sum / mAudioBuffer.length);
final double db = 20 * Math.log10(rms);
// Update the text view on the main thread.
tvdb.post(new Runnable() {
@Override
public void run() {
tvdb.setText(String.format(mDecibelFormat, db));
}
});
}
}
/// post the output frequency to TextView
private void updateFrequency() {
tvfreq.post(new Runnable() {
@Override
public void run() {
NumberFormat nM = NumberFormat.getNumberInstance();
tvfreq.setText(nM.format(mFreqFormat) + " hz");
}
});
}
最佳答案
您的代码有几个问题,但最重要的一个是您的峰值查找循环完全中断 - 更改:
double peak = -1.0;
// Get the largest magnitude peak
for (int i = 0; i < blockSize; i++) {
peak = magnitude[i];
}
到:
double peak_val = magnitude[0]; // init magnitude of peak
peak = 0; // init index of peak
for (int i = 1; i < blockSize; i++) {
double val = magnitude[i];
if (val > peak_val) {
peak_val = val; // update magnitude of peak
peak = i; // update index of peak
}
}
关于android - Jtransforms,输出频率不准确。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24973642/
我有一个包含 34 个变量和大约 25,000 个观测值的数据集。每个观察都涉及一个特定事件。它的格式如下: no id date .... 1 363 006
我已将 R 连接到 Twitter 并使用 R 中的 searchTwitter 函数进行抓取,并清除标点符号、小写字母等结果数据。现在我正在尝试执行以下操作: 计算自 2015 年 1 月 至 20
我正在研究项目,需要可视化频谱分析以设置一些精确参数。现在,我将垃圾箱转换为屏幕空间,因为在线性空间中,较低频率的幅度被压缩在一起。这是我在C++中的代码: float windowSize = 64
我正在尝试使用MATLAB导入WAV文件并创建如下所示的图表类型。我基本上是在尝试获取频率信息并根据分贝对其进行绘制。这是我正在使用的代码,但似乎无法正确提取频率信息: [x fs]=wavread(
我有一个 GUI,可以计算字符串中第一个字母的出现次数。我希望它以列格式计算所有字母,例如: 这是我到目前为止所拥有的: import java.awt.BorderLayout; import ja
我有一个由许多变量组成的全国调查,就像这个(为了简单起见,我省略了一些变量): year id y.b sex income married pens weight 2002
我被要求报告我们客户的联系频率,即每周、每月、每季度或每年看到多少客户。 当在论坛中讨论“频率”时,它们通常是指某个值在表中存在的次数。 我可以获得客户的联系人数量:- select A.cl
我正在尝试制作一款游戏,当麦克风发出足够响亮的声音时,我的角色会射击(在 Unity 中)。但是我不知道如何开始。 感谢您的帮助! 最佳答案 您可以通过使用 AudioSource.GetOutput
尝试计算字符数并改进我的代码,我做了一些更改,而不是使用 while 循环。好奇是否有人对我如何改进我的代码以使其更专业且更便宜有任何建议? #include int countingCharact
我正在创建一个 MySQL 数据库,其中包含大量带有时间戳的条目。这些条目将附加到特定用户和另一个索引(例如博客作者和他的几个网站)。计算用户/全局每日条目图表的最佳方法是什么。 我的两种方法是使用
我创建了一项调查并将其发送出去。该调查要求用户提供电子邮件,然后要求他们从包含 8 个不同选项的下拉菜单中选择要吃哪顿饭。有些人使用同一封电子邮件多次填写调查,但食物选择不同。 我有一个如下所示的 M
我有一个 MySQL 数据库: Date Customer_ID 我怎样才能把它变成: Customer_ID | Count_Visits_Past_Week | Count_Visits_
对于非常大的数据集,如何使用 gnuplot 仅在第一个和最后一个数据点的 x 轴上放置标记/标签? 最佳答案 在 gnuplot 4.6 及更高版本中,您可以使用命令 stats 'data.dat
我正在寻找一种方法来为具有共同词根/含义的单个词生成数值概率值。 用户将使用“舞者”、“跳舞”、“跳舞”等词生成内容。 如果“dancer”被提交了 30 次,跳舞了 5 次,我只需要一个值“danc
给定一个包含如下内容的数据集: [2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 65, 75, 85, 86, 87,
我想将声音的音高绘制成图表。 目前我可以绘制幅度。下图是由 getUnscaledAmplitude() 返回的数据创建的: AudioInputStream audioInputStream = A
在 Javascript 中,我试图获取一个初始的数值数组并计算其中的元素。理想情况下,结果将是两个新数组,第一个指定每个唯一元素,第二个包含每个元素出现的次数。不过,我愿意接受有关输出格式的建议。
我正在编写一个多线程OpenMPI应用程序,使用来自多个线程的MPI_Isend和MPI_Irecv在InfiniBand RDMA的各个列之间每秒交换数百条消息。 传输量约为400-800KByte
这个站点上有很多问题,询问如何在给定频率下创建简单的正弦波。我想做的是获取阵列或列表或任何频率,然后连续连续播放它们(而不是和弦),听起来有点像旧PC扬声器。我尝试使用Console.Beep,但是它
我使用我的App捕获声音。假设此声音是正弦1 KHz声音,并且存在背景声音。如何识别此1 KHz声音出现在声音上? 我的意思是,我可以想象如何在图像中找到元素,例如,如果您要在图像上寻找黄色正方形,那
我是一名优秀的程序员,十分优秀!