我正在尝试修改生成方波的方法
它可以产生八个脉冲,每个脉冲具有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;
}
}
这是我针对几乎相同问题的解决方案。
在我的情况下,我创建了宽度为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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句