我正在尝试修改生成方波的方法
它可以产生8个不同脉冲宽度的脉冲,每个周期延迟0.3ms。我已经看到了 sampleBuffer负责生成脉冲信号,但我不知道如何为这种特定模式创建脉冲函数。您能告诉我AudioTrack.h是否有用于生成脉冲的库函数吗?
以下是我的
代码生成方波
void generateSquare(SInt16 *sampleBuffer, int numFrames, float sampleRate, float frequency, float amp) {
if(amp>1) amp=1;
if(amp<0) amp=0;
amp = amp*SHRT_MAX;
float samplesPerCycle = sampleRate/frequency;
for(int i = 0; i < numFrames; i++) {
if(fmodf(squareIndex, samplesPerCycle)/samplesPerCycle > 0.5) {
sampleBuffer[i] = amp;
} else {
sampleBuffer[i] = -1*amp;
}
squareIndex = squareIndex+1;
if(squareIndex >= samplesPerCycle) squareIndex-=samplesPerCycle;
}
}
答案 0 :(得分:1)
这是我几乎同样问题的解决方案。
在我的情况下,我创建宽度为1ms的脉冲,我用填充值修改+/- 0.5ms。
因此,根据fillValue,我产生一个脉冲宽度为0.5-1.5ms的方波。
int squareIndex = 0;
void generateSquare(SInt16 *sampleBuffer, int numFrames, float sampleRate, float fillValue, float amp) {
// Fill value = pulse width value in frames
// fillValue = [-20, 20];
if(amp>1) amp=1;
if(amp<0) amp=0;
if(fillValue > 20) fillValue = 20;
if(fillValue < -20) fillValue = -20;
amp = amp*SHRT_MAX;
float samplesPerCycle = sampleRate/50;
//Sample / Cycle = 882
//1ms = 41 frame -> 0.5ms = 20(.5)frame
//In your case 0.3ms = 12(.3) frame
#pragma mark - PWM
for(int i = 0; i < numFrames; i++) {
//if(fmodf(squareIndex, samplesPerCycle)/samplesPerCycle < 0.05) {
if(squareIndex < 41 + fillValue) {
sampleBuffer[i] = 1*SHRT_MAX;
} else {
sampleBuffer[i] = 0;
}
squareIndex = squareIndex+1;
if(squareIndex >= samplesPerCycle) squareIndex-=samplesPerCycle;
}
}