Skip to content

Commit 377d524

Browse files
committed
Update to version 1.8.1.
1 parent 619f608 commit 377d524

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

src/audioConfig.c

+45-35
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454

5555
#define VCO_GAIN 0.5f
5656

57-
#define AGC_FILTER_CUTOFF_FREQUENCY 20
57+
#define USE_AGC true
58+
#define MANUAL_GAIN_DIVISOR 5000.0f
59+
#define AGC_MINIMUM_AMPLITUDE 100.0f
60+
#define AGC_FILTER_CUTOFF_FREQUENCY 1000
61+
5862
#define CHANNEL_FILTER_CUTOFF_FREQUENCY 100
5963
#define CHANNEL_FILTER_BANDWIDTH 2.0f
6064
#define CARRIER_FILTER_CUTOFF_FREQUENCY 1000
@@ -75,6 +79,8 @@
7579

7680
#define MIN(a, b) ((a) < (b) ? (a) : (b))
7781

82+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
83+
7884
/* Sine table */
7985

8086
static const float sineTable[SINE_TABLE_LENGTH] = {0.000000000000f, 0.024541229010f, 0.049067676067f, 0.073564566672f, 0.098017141223f, 0.122410677373f, 0.146730467677f, 0.170961901546f, \
@@ -150,7 +156,7 @@ static BQ_filterCoefficients_t channelFilterCoefficients;
150156

151157
/* Carrier generation variable */
152158

153-
float omegaT = 0.0f;
159+
static float omegaT = 0.0f;
154160

155161
/* Configuration variables */
156162

@@ -170,7 +176,7 @@ typedef enum {NONE, HIGH_BIT, LOW_BIT} receivedBit_t;
170176

171177
/* CRC functions */
172178

173-
static uint16_t updateCRC(uint16_t crc_in, int incr) {
179+
static inline uint16_t updateCRC(uint16_t crc_in, int incr) {
174180

175181
uint16_t xor = crc_in >> 15;
176182
uint16_t out = crc_in << 1;
@@ -187,7 +193,7 @@ static uint16_t updateCRC(uint16_t crc_in, int incr) {
187193

188194
}
189195

190-
static uint16_t calculateCRC(const uint8_t *data, uint32_t size) {
196+
static inline uint16_t calculateCRC(const uint8_t *data, uint32_t size) {
191197

192198
uint16_t crc, i;
193199

@@ -205,7 +211,7 @@ static uint16_t calculateCRC(const uint8_t *data, uint32_t size) {
205211

206212
}
207213

208-
static bool checkCRC(const uint8_t *data, uint32_t size) {
214+
static inline bool checkCRC(const uint8_t *data, uint32_t size) {
209215

210216
uint16_t crc = calculateCRC(data, size - CRC_SIZE_IN_BYTES);
211217

@@ -218,15 +224,25 @@ static bool checkCRC(const uint8_t *data, uint32_t size) {
218224

219225
/* Function to perform Costas loop */
220226

221-
float updateCostasLoop(float sample) {
227+
static inline float updateCostasLoop(float sample) {
222228

223229
/* Apply gain control */
224230

225231
float filteredSample = Butterworth_applyBandPassFilter(sample, &carrierFilter, &carrierFilterCoefficients);
226232

227-
float agcOutput = Butterworth_applyLowPassFilter(filteredSample > 0 ? filteredSample : -filteredSample, &agcFilter, &agcFilterCoefficients);
233+
if (USE_AGC) {
234+
235+
float regulatedFilteredSample = filteredSample > 0.0f ? filteredSample : -filteredSample;
236+
237+
float agcOutput = Butterworth_applyLowPassFilter(regulatedFilteredSample, &agcFilter, &agcFilterCoefficients);
238+
239+
filteredSample /= MAX(agcOutput, AGC_MINIMUM_AMPLITUDE);
228240

229-
if (agcOutput != 0) filteredSample /= agcOutput;
241+
} else {
242+
243+
filteredSample /= MANUAL_GAIN_DIVISOR;
244+
245+
}
230246

231247
/* Demodulate input sound */
232248

@@ -258,10 +274,10 @@ float updateCostasLoop(float sample) {
258274

259275
inline void AudioMoth_handleMicrophoneInterrupt(int16_t sample) {
260276

261-
configSample = sample;
262-
263277
configSampleReady = true;
264278

279+
configSample = sample;
280+
265281
}
266282

267283
/* Functions to handle audio configuration */
@@ -272,28 +288,30 @@ void AudioConfig_enableAudioConfiguration() {
272288

273289
AudioMoth_enableMicrophone(CONFIG_GAIN_RANGE, CONFIG_GAIN, CONFIG_CLOCK_DIVIDER, CONFIG_ACQUISITION_CYCLES, CONFIG_OVERSAMPLE_RATE);
274290

275-
AudioMoth_startMicrophoneSamples(CONFIG_SAMPLE_RATE);
276-
277-
AudioMoth_initialiseMicrophoneInterupts();
291+
AudioMoth_initialiseMicrophoneInterrupts();
278292

279293
/* Design filters */
280294

281-
Butterworth_designLowPassFilter(&agcFilterCoefficients, CONFIG_SAMPLE_RATE, SPEED_FACTOR * AGC_FILTER_CUTOFF_FREQUENCY);
282-
283295
Butterworth_designBandPassFilter(&carrierFilterCoefficients, CONFIG_SAMPLE_RATE, CONFIG_CARRIER_FREQUENCY - CARRIER_FILTER_CUTOFF_FREQUENCY, CONFIG_CARRIER_FREQUENCY + CARRIER_FILTER_CUTOFF_FREQUENCY);
284296

297+
if (USE_AGC) Butterworth_designLowPassFilter(&agcFilterCoefficients, CONFIG_SAMPLE_RATE, SPEED_FACTOR * AGC_FILTER_CUTOFF_FREQUENCY);
298+
285299
Biquad_designLowPassFilter(&channelFilterCoefficients, CONFIG_SAMPLE_RATE, SPEED_FACTOR * CHANNEL_FILTER_CUTOFF_FREQUENCY, CHANNEL_FILTER_BANDWIDTH);
286300

287301
/* Initialise filters */
288302

289-
Butterworth_initialise(&agcFilter);
290-
291303
Butterworth_initialise(&carrierFilter);
292304

305+
if (USE_AGC) Butterworth_initialise(&agcFilter);
306+
293307
Biquad_initialise(&channel1Filter);
294308

295309
Biquad_initialise(&channel2Filter);
296310

311+
/* Start the microphone samples */
312+
313+
AudioMoth_startMicrophoneSamples(CONFIG_SAMPLE_RATE);
314+
297315
}
298316

299317
void AudioConfig_disableAudioConfiguration() {
@@ -344,7 +362,7 @@ bool AudioConfig_listenForAudioConfigurationTone(uint32_t milliseconds) {
344362

345363
/* Check thresholds */
346364

347-
if ((lastValue > 0 && costasLoopOutput < 0) || (lastValue < 0 && costasLoopOutput > 0)) {
365+
if ((lastValue >= 0 && costasLoopOutput < 0) || (lastValue < 0 && costasLoopOutput >= 0)) {
348366

349367
uint32_t period = counter - lastCrossing;
350368

@@ -368,20 +386,16 @@ bool AudioConfig_listenForAudioConfigurationTone(uint32_t milliseconds) {
368386

369387
}
370388

389+
/* Update counters and status */
390+
371391
lastValue = costasLoopOutput;
372392

393+
configSampleReady = false;
394+
373395
counter += 1;
374396

375397
}
376398

377-
/* Wait for next sample */
378-
379-
configSampleReady = false;
380-
381-
/* Sleep until next interrupt occurs */
382-
383-
AudioMoth_sleep();
384-
385399
}
386400

387401
return false;
@@ -434,7 +448,7 @@ bool AudioConfig_listenForAudioConfigurationPackets(bool timeout, uint32_t milli
434448

435449
/* Check thresholds */
436450

437-
if ((lastValue > 0 && costasLoopOutput < 0) || (lastValue < 0 && costasLoopOutput > 0)) {
451+
if ((lastValue >= 0 && costasLoopOutput < 0) || (lastValue < 0 && costasLoopOutput >= 0)) {
438452

439453
uint32_t period = counter - lastCrossing;
440454

@@ -606,20 +620,16 @@ bool AudioConfig_listenForAudioConfigurationPackets(bool timeout, uint32_t milli
606620

607621
}
608622

623+
/* Update counters and status */
624+
609625
lastValue = costasLoopOutput;
610626

627+
configSampleReady = false;
628+
611629
counter += 1;
612630

613631
}
614632

615-
/* Wait for next sample */
616-
617-
configSampleReady = false;
618-
619-
/* Sleep until next interrupt occurs */
620-
621-
AudioMoth_sleep();
622-
623633
}
624634

625635
return cancel == false;

src/main.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ static int16_t secondaryBuffer[MAXIMUM_SAMPLES_IN_DMA_TRANSFER];
911911

912912
/* Firmware version and description */
913913

914-
static uint8_t firmwareVersion[AM_FIRMWARE_VERSION_LENGTH] = {1, 8, 0};
914+
static uint8_t firmwareVersion[AM_FIRMWARE_VERSION_LENGTH] = {1, 8, 1};
915915

916916
static uint8_t firmwareDescription[AM_FIRMWARE_DESCRIPTION_LENGTH] = "AudioMoth-Firmware-Basic";
917917

@@ -1031,7 +1031,7 @@ static GPS_fixResult_t setTimeFromGPS(bool enableLED, uint32_t timeout) {
10311031

10321032
}
10331033

1034-
/* Magenetic switch wait functions */
1034+
/* Magnetic switch wait functions */
10351035

10361036
static void startWaitingForMagneticSwith() {
10371037

@@ -2345,7 +2345,7 @@ static AM_recordingState_t makeRecording(uint32_t timeOfNextRecording, uint32_t
23452345

23462346
/* Calculate time correction for sample rate due to file header */
23472347

2348-
uint32_t numberOfSamplesInHeader = sizeof(wavHeader) / NUMBER_OF_BYTES_IN_SAMPLE;
2348+
uint32_t numberOfSamplesInHeader = sizeof(wavHeader_t) / NUMBER_OF_BYTES_IN_SAMPLE;
23492349

23502350
int32_t sampleRateTimeOffset = ROUNDED_DIV(numberOfSamplesInHeader * MILLISECONDS_IN_SECOND, effectiveSampleRate);
23512351

@@ -2375,7 +2375,7 @@ static AM_recordingState_t makeRecording(uint32_t timeOfNextRecording, uint32_t
23752375

23762376
/* Calculate updated recording parameters */
23772377

2378-
uint32_t maximumNumberOfSeconds = (MAXIMUM_WAV_FILE_SIZE - sizeof(wavHeader)) / NUMBER_OF_BYTES_IN_SAMPLE / effectiveSampleRate;
2378+
uint32_t maximumNumberOfSeconds = (MAXIMUM_WAV_FILE_SIZE - sizeof(wavHeader_t)) / NUMBER_OF_BYTES_IN_SAMPLE / effectiveSampleRate;
23792379

23802380
bool fileSizeLimited = (recordDuration > maximumNumberOfSeconds);
23812381

@@ -2469,11 +2469,11 @@ static AM_recordingState_t makeRecording(uint32_t timeOfNextRecording, uint32_t
24692469

24702470
while (numberOfBlankSamplesToWrite > 0) {
24712471

2472-
uint32_t numberOfSmples = MIN(numberOfBlankSamplesToWrite, COMPRESSION_BUFFER_SIZE_IN_BYTES / NUMBER_OF_BYTES_IN_SAMPLE);
2472+
uint32_t numberOfSamples = MIN(numberOfBlankSamplesToWrite, COMPRESSION_BUFFER_SIZE_IN_BYTES / NUMBER_OF_BYTES_IN_SAMPLE);
24732473

2474-
FLASH_LED_AND_RETURN_ON_ERROR(AudioMoth_writeToFile(compressionBuffer, NUMBER_OF_BYTES_IN_SAMPLE * numberOfSmples));
2474+
FLASH_LED_AND_RETURN_ON_ERROR(AudioMoth_writeToFile(compressionBuffer, NUMBER_OF_BYTES_IN_SAMPLE * numberOfSamples));
24752475

2476-
numberOfBlankSamplesToWrite -= numberOfSmples;
2476+
numberOfBlankSamplesToWrite -= numberOfSamples;
24772477

24782478
}
24792479

@@ -2554,7 +2554,7 @@ static AM_recordingState_t makeRecording(uint32_t timeOfNextRecording, uint32_t
25542554

25552555
FLASH_LED_AND_RETURN_ON_ERROR(AudioMoth_seekInFile(0));
25562556

2557-
FLASH_LED_AND_RETURN_ON_ERROR(AudioMoth_writeToFile(&wavHeader, sizeof(wavHeader)));
2557+
FLASH_LED_AND_RETURN_ON_ERROR(AudioMoth_writeToFile(&wavHeader, sizeof(wavHeader_t)));
25582558

25592559
/* Close the file */
25602560

0 commit comments

Comments
 (0)