
我真的不知道从那些量级中读到什么.如何从这些量级解码摩尔斯电码?我怎么读它们?试图找到参考,但没有解释什么是结果以及如何阅读它.
编辑:
我的摩尔斯电码应用程序是用Delphi编写的,并使用Windows Beep函数以特定频率发送信号.我使用1200赫兹的信号.信号和单词之间也会暂停,莫尔斯的哔哔声就像维基百科所描述的那样.一切都准确.
Goertzel.java:
public class Goertzel { private float samplingRate; private float targetFrequency; private int n; private double coeff,Q1,Q2; private double sine,cosine; public Goertzel(float samplingRate,float targetFrequency,int inN) { this.samplingRate = samplingRate; this.targetFrequency = targetFrequency; n = inN; sine = Math.sin(2 * Math.PI * (targetFrequency / samplingRate)); cosine = Math.cos(2 * Math.PI * (targetFrequency / samplingRate)); coeff = 2 * cosine; } public voID resetGoertzel() { Q1 = 0; Q2 = 0; } public voID initGoertzel() { int k; float floatN; double omega; floatN = (float) n; k = (int) (0.5 + ((floatN * targetFrequency) / samplingRate)); omega = (2.0 * Math.PI * k) / floatN; sine = Math.sin(omega); cosine = Math.cos(omega); coeff = 2.0 * cosine; resetGoertzel(); } public voID processSample(double sample) { double Q0; Q0 = coeff * Q1 - Q2 + sample; Q2 = Q1; Q1 = Q0; } public double[] getRealimag(double[] parts) { parts[0] = (Q1 - Q2 * cosine); parts[1] = (Q2 * sine); return parts; } public double getMagnitudeSquared() { return (Q1 * Q1 + Q2 * Q2 - Q1 * Q2 * coeff); } } SoundCompareActivity.java
import java.io.file;import java.io.fileNotFoundException;import java.io.fileOutputStream;import java.io.IOException;import androID.app.Activity;import androID.media.AudioFormat;import androID.media.AudioRecord;import androID.media.MediaRecorder;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button; public class SoundCompareActivity extends Activity { private static final int RECORDER_SAMPLE_RATE = 8000; // at least 2 times // higher than sound // frequency,private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_CONfigURATION_MONO; private static final int RECORDER_AUdio_ENCoding = AudioFormat.ENCoding_PCM_16BIT; private AudioRecord recorder = null; private int bufferSize = 0; private Thread recordingThread = null; private boolean isRecording = false; private button startRecBtn; private button stopRecBtn; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); startRecBtn = (button) findVIEwByID(R.ID.button1); stopRecBtn = (button) findVIEwByID(R.ID.button2); startRecBtn.setEnabled(true); stopRecBtn.setEnabled(false); bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE,RECORDER_CHANNELS,RECORDER_AUdio_ENCoding); startRecBtn.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Log.d("SOUNDCOMPARE","Start Recording"); startRecBtn.setEnabled(false); stopRecBtn.setEnabled(true); stopRecBtn.requestFocus(); startRecording(); } }); stopRecBtn.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Log.d("SOUNDCOMPARE","Stop recording"); startRecBtn.setEnabled(true); stopRecBtn.setEnabled(false); startRecBtn.requestFocus(); stopRecording(); } }); } private voID startRecording() { recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLE_RATE,RECORDER_AUdio_ENCoding,bufferSize); recorder.startRecording(); isRecording = true; recordingThread = new Thread(new Runnable() { @OverrIDe public voID run() { writeAudioDataToTempfile(); } },"AudioRecorder Thread"); recordingThread.start(); } private String getTempfilename() { file file = new file(getfilesDir(),"tempaudio"); if (!file.exists()) { file.mkdirs(); } file tempfile = new file(getfilesDir(),"signal.raw"); if (tempfile.exists()) tempfile.delete(); return (file.getabsolutePath() + "/" + "signal.raw"); } private voID writeAudioDataToTempfile() { byte data[] = new byte[bufferSize]; String filename = getTempfilename(); fileOutputStream os = null; try { os = new fileOutputStream(filename); } catch (fileNotFoundException e) { e.printstacktrace(); } int read = 0; if (os != null) { while (isRecording) { read = recorder.read(data,bufferSize); if (read != AudioRecord.ERROR_INVALID_OPERATION) { try { os.write(data); } catch (IOException e) { e.printstacktrace(); } } } try { os.close(); } catch (IOException e) { e.printstacktrace(); } } } private voID deleteTempfile() { file file = new file(getTempfilename()); file.delete(); } private voID stopRecording() { if (recorder != null) { isRecording = false; recorder.stop(); recorder.release(); recorder = null; recordingThread = null; } new MorseDecoder().execute(new file(getTempfilename())); } } MorseDecoder.java:
import java.io.file;import java.io.fileinputStream;import java.io.fileNotFoundException;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.ShortBuffer;import androID.media.AudioFormat;import androID.media.AudioRecord;import androID.os.AsyncTask;import androID.util.Log;public class MorseDecoder extends AsyncTask<file,VoID,VoID> { private fileinputStream is = null; @OverrIDe protected VoID doInBackground(file... files) { int index; //double magnitudeSquared; double magnitude; int bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONfigURATION_MONO,AudioFormat.ENCoding_PCM_16BIT); Goertzel g = new Goertzel(8000,1200,bufferSize); g.initGoertzel(); for (int i = 0; i < files.length; i++) { byte[] data = new byte[bufferSize]; try { is = new fileinputStream(files[i]); while(is.read(data) != -1) { ShortBuffer sbuf = ByteBuffer.wrap(data).order(ByteOrder.liTTLE_ENDIAN).asShortBuffer(); short[] audioShorts = new short[sbuf.capacity()]; sbuf.get(audioShorts); float[] audiofloats = new float[audioShorts.length]; for (int j = 0; j < audioShorts.length; j++) { audiofloats[j] = ((float)audioShorts[j]) / 0x8000; } for (index = 0; index < audiofloats.length; index++) { g.processSample(data[index]); } magnitude = Math.sqrt(g.getMagnitudeSquared()); Log.d("SoundCompare","relative magnitude = " + magnitude); g.resetGoertzel(); } is.close(); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } return null; }} EDIT2:
注意处理样本中的一些错误.在while循环中更改了代码.
while(is.read(data) != -1) { ShortBuffer sbuf = ByteBuffer.wrap(data).order(ByteOrder.liTTLE_ENDIAN).asShortBuffer(); short[] audioShorts = new short[sbuf.capacity()]; sbuf.get(audioShorts); float[] audiofloats = new float[audioShorts.length]; for (int j = 0; j < audioShorts.length; j++) { audiofloats[j] = ((float)audioShorts[j]) / 0x8000; } for (index = 0; index < audiofloats.length; index++) { g.processSample(audiofloats[index]); magnitude = Math.sqrt(g.getMagnitudeSquared()); Log.d("SoundCompare","relative magnitude = " + magnitude); } //magnitude = Math.sqrt(g.getMagnitudeSquared()); //Log.d("SoundCompare","relative magnitude = " + magnitude); g.resetGoertzel(); } 问候,
恶者
以上是内存溢出为你收集整理的android – 使用Goertzel算法处理音频信号的结果全部内容,希望文章能够帮你解决android – 使用Goertzel算法处理音频信号的结果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)