Skip to content

Commit 67560c8

Browse files
committed
fix a bug in fm-demo math
- fix a sign error in fm demodulation math - remove (-Werror) from build options; turn off trace debug output - reduce sample rate back to 240k and 16khz audio
1 parent a103f2a commit 67560c8

File tree

7 files changed

+22
-14
lines changed

7 files changed

+22
-14
lines changed

builder/options.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CFLAGS += $(ARCH)
99

1010
# compile using c11, all warnings, output listing
1111
C_APP_FLAGS += -std=c11
12-
C_APP_FLAGS += -Werror -Wall -Wextra
12+
C_APP_FLAGS += -Wall -Wextra
1313
C_APP_FLAGS += -Wa,-ashld=$(LST_DIR)/$(@F:.o=.lst)
1414

1515
# ST HAL settings

source/application/audio_player.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int8_t audio_init(void) {
77
self.volume = 70;
88
self.is_playing = 0;
99

10-
if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, self.volume, I2S_AUDIOFREQ_32K) == AUDIO_OK) {
10+
if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, self.volume, I2S_AUDIOFREQ_16K) == AUDIO_OK) {
1111

1212
BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
1313

source/application/fm_radio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ void fmradio_process() {
7777
s2[1] = (int16_t)raw_buf_complete[sample_index + 3] - 127; // Q
7878

7979
// find the phase shift in these two samples (this is the essence of the FM demod)
80-
int16_t pcm = fmradio_polar_disc(s2[0], s2[1],
81-
s1[0], s1[1]);
80+
int16_t pcm = fmradio_polar_disc(s2[0], s2[1], s1[0], s1[1]);
8281

8382
pcm = pcm * 8.0f / PI; // scale from radians
8483

source/application/fm_radio_math_utils.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ int32_t fmradio_atan2(int32_t y, int32_t x) {
3232
// fm demodulation math taken from librtlsdr
3333
int32_t fmradio_polar_disc(int32_t ar, int32_t aj, int32_t br, int32_t bj) {
3434

35-
int32_t cr, cj;
36-
37-
// complex multiply
38-
cr = ar * br - aj * bj;
39-
cj = aj * br + ar * bj;
35+
// complex multiply first sample with
36+
// second sample flipped over the real axis
37+
// (ar+aj) * (br-bj)
38+
int32_t cr = ar * br - aj * -bj;
39+
int32_t cj = aj * br + ar * -bj;
4040

41+
// find angle of resultant
4142
return fmradio_atan2(cj, cr);
43+
4244
}

source/application/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,14 @@ static void SystemClock_Config(void) {
147147
}
148148
}
149149

150-
void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef* hsai, uint32_t AudioFreq, void* Params) {
150+
void BSP_AUDIO_OUT_ClockConfig(SAI_HandleTypeDef* hsai,
151+
uint32_t AudioFreq,
152+
void* Params)
153+
{
154+
155+
UNUSED(Params);
156+
UNUSED(hsai);
157+
151158
RCC_PeriphCLKInitTypeDef RCC_ExCLKInitStruct;
152159

153160
HAL_RCCEx_GetPeriphCLKConfig(&RCC_ExCLKInitStruct);

source/application/main.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#include "fm_radio.h"
1111
#include "audio_player.h"
1212

13-
#define USB_PIPE_NUMBER (0x81)
13+
#define USB_PIPE_NUMBER 0x81
1414
#define KILOBYTES 1024
1515
#define RAW_BUFFER_BYTES (25*KILOBYTES)
1616
#define SIZEOF_DEMOD_BUF_EL 2
1717
#define DEMOD_BUFF_BYTES (RAW_BUFFER_BYTES/SIZEOF_DEMOD_BUF_EL)
18-
#define DOWNSAMPLE 30
19-
#define RTL_SAMPLERATE 960000
18+
#define DOWNSAMPLE 15
19+
#define RTL_SAMPLERATE 240000
2020

2121
volatile uint8_t raw_bufA[RAW_BUFFER_BYTES];
2222
volatile uint8_t raw_bufB[RAW_BUFFER_BYTES];

source/application/trace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "stm32f7xx_hal.h"
55

6-
#if 1
6+
#if 0
77
#define DEBUG_PRINT(...) printf(__VA_ARGS__)
88
#else
99
#define DEBUG_PRINT(...)

0 commit comments

Comments
 (0)