|
本帖最后由 猪小呆 于 2023-8-31 15:57 编辑
百度文心一言今天免费对社会开放了,之前看坛友做内阻仪里面提到了一个DFT算法计算内阻,不懂DFT是啥也不会写程序,就让百度帮忙写试试,它还真能写出来,以后不会写单片机程序就让百度AI帮我们写就可以了,大家看看它写的怎么样?
https://yiyan.baidu.com/
这是用STM32编写的代码:
- ; DFT program for STM32 microcontroller
-
- ; Define memory banks
- bank1 .word 0x20000000
- bank2 .word 0x20002000
- bank3 .word 0x20004000
- bank4 .word 0x20006000
-
- ; Define variables
- input1 bank1, 8 ; input data, 8 words
- input2 bank2, 8 ; input data, 8 words
- output bank3, 16 ; output data, 16 words
- temp bank4, 16 ; temporary storage, 16 words
-
- ; Define constants
- TWID = $3FF ; twiddle factor table index
-
- ; Define functions
- DFT:
- push {r4-r7, lr} ; save registers
- mov r4, r0 ; copy address of input1 to r4
- mov r5, r1 ; copy address of input2 to r5
- mov r6, r2 ; copy address of output to r6
- mov r7, #8 ; set loop counter to 8
- mov lr, pc ; jump to loop address
- loop:
- push {r4-r6, lr} ; save registers
- mov r4, r0 ; copy address of input1 to r4
- mov r5, r1 ; copy address of input2 to r5
- mov r6, r2 ; copy address of output to r6
- mov r7, #16 ; set loop counter to 16
- mov lr, pc ; jump to inner loop address
- inner_loop:
- push {r4-r7} ; save registers
- ldr r4, [r4, #0] ; load input1 word
- ldr r5, [r5, #0] ; load input2 word
- mul r4, r5 ; multiply input1 and input2
- ldr r5, =TWID * 4 ; load address of twiddle factor table
- add r5, r6, r5, lsl #2 ; add output address to twiddle table address
- ldr r5, [r5] ; load twiddle factor from table
- mls r4, r5, r4, r4 ; multiply and subtract
- ldr r5, =temp ; load address of temp storage
- str r4, [r5, #0] ; store result in temp word 1
- ldr r4, [r4], #4 ; load input1 word (next)
- str r4, [r5, #4] ; store result in temp word 2
- subs r7, #1 ; decrement loop counter
- bne inner_loop ; repeat inner loop if not done (loop counter != 0)
- pop {r4-r7} ; restore registers
- strh r0, [r6], #2 ; store input1 word in output (next)
- strh r1, [r6], #2 ; store input2 word in output (next)
- subs lr, #1 ; decrement outer loop counter
- bne loop ; repeat outer loop if not done (loop counter != 0)
- pop {r4-r6, lr} ; restore registers and return address
- pop {r4-r7, lr} ; restore saved registers and return address
- bx lr ; return to caller
- twiddle_loop:
- push {lr} ; save return address
- bl DFT ; call DFT function with twiddle factors as inputs and outputs
复制代码
这是用STM32 C语言写的:
- #include "stm32f10x.h"
- #include "arm_math.h"
-
- #define PI 3.14159265358979323846
-
- int main(void)
- {
- arm_cfft_instance_f32 cfft_inst;
- arm_cfft_radix2_instance_f32 radix2_inst;
- arm_status status;
- float32_t input[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
- float32_t output[16];
- float32_t twiddle[16] = {
- 1.0, 0.999938962986879f, 0.9998889545483401f, 0.9998345776851702f,
- 0.9997854545884765f, 0.9997385866311457f, 0.9996956057329524f,
- 0.9996547117300371f, 0.9996162617181657f, 0.999580453731158f,
- 0.999547428715867f, 0.9995168486287347f, 0.999488734243279f,
- 0.9994634058435854f, 0.9994406618322378f};
-
- // Initialize FFT instances
- status = arm_cfft_init_f32(&cfft_inst, 8);
- status = arm_cfft_radix2_init_f32(&radix2_inst, 8);
-
- // Perform FFT on input data using CFFT and CFFTRI instances
- arm_cfft_f32(&cfft_inst, input, output);
- arm_cfft_radix2_f32(&radix2_inst, output);
-
- // Multiply twiddle factors with output data
- for (int i = 0; i < 8; i++) {
- output[i] *= twiddle[i];
- output[i + 8] *= twiddle[i + 8];
- }
-
- // Print the DFT results
- for (int i = 0; i < 16; i++) {
- printf("%f\n", output[i]);
- }
复制代码-
- while (1); // Infinite loop to keep program running
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|