- Edited
I wanted to change the analog sampling rate to 8000 Hz. I found a post regarding this here, which says for changing from 44.1 kHz to 8 kHz;
sed -i s/44100/8000/g /root/Bela/core/*_Codec.cpp
I ran the command in the Bela IDE and found this as error so i reverted back to 44.1kHz.
I am attaching the code which i ran. Can anyone help regarding this ?
#include <Bela.h>
#include <cmath>
#include <iostream>
#include <libraries/Scope/Scope.h>
#include <libraries/Biquad/Biquad.h>
Scope scope;
float amplitude;
float gFrequency = 600.0;
float gPhase;
float gInverseSampleRate;
int gAudioFramesPerAnalogFrame = 0;
int InChannel = 0;
Biquad lpFilter; // Biquad low-pass frequency;
Biquad hpFilter; // Biquad high-pass frequency;
float gHPfreq = 200.0; // Cut-off frequency for high-pass filter (Hz)
float gLPfreq = 800.0; // Cut-off frequency for low-pass filter (Hz)
float gFilterQ = 0.5; // Quality factor for the biquad filters to provide a Butterworth response (0.707 default)
// Variables for DC bias removal
float gDcBias = 0.0;
// DC bias removal
float DcBiasRemove(float rawValue, float &gDcBias) {
const float gDcBiasAlpha = 0.99;
// Remove DC bias using a simple low-pass filter
gDcBias = gDcBiasAlpha * gDcBias + (1.0 - gDcBiasAlpha) * rawValue;
float amplitude = rawValue - gDcBias;
return amplitude;
}
bool setup(BelaContext *context, void *userData)
{
// setup the scope with 1 channel at the audio sample rate
scope.setup(3, context->audioSampleRate);
// Low pass filter settings
Biquad::Settings settings{
.fs = context->audioSampleRate,
.type = Biquad::lowpass,
.cutoff = gLPfreq,
.q = gFilterQ,
.peakGainDb = 0,
};
lpFilter.setup(settings);
settings.cutoff = gHPfreq;
settings.type = Biquad::highpass;
hpFilter.setup(settings);
// Check if analog channels are enabled
if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
return false;
}
// Useful calculations
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
float out = sinf(gPhase);
gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
audioWrite(context, n, channel, out);
}
// static float frequency;
if(gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) {
amplitude = analogRead(context, n/gAudioFramesPerAnalogFrame, InChannel);
}
//amplitude = DcBiasRemove(amplitude, gDcBias);
float amplitude1 = hpFilter.process(amplitude);
amplitude1 = lpFilter.process(amplitude1);
// log the sine wave and sensor values on the scope
scope.log(200*amplitude1, 200*amplitude, out);
}
}
void cleanup(BelaContext *context, void *userData)
{
}