硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1099|回复: 7
收起左侧

[DSP] 研究了下Matlab Simulink生成算法工程,代码太复杂了,不好魔改自己使用

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106934
QQ
发表于 2022-3-24 00:57:05 | 显示全部楼层 |阅读模式
有个这个Fir滤波的例子:

image.png

生成的代码太复杂了,看着非常吃力,生成了好几个文件。

[C] 纯文本查看 复制代码
/*
 * File: stm32f4discovery_cmsis_crl.c
 *
 * Code generated for Simulink model 'stm32f4discovery_cmsis_crl'.
 *
 * Model version                  : 1.34
 * Simulink Coder version         : 8.14 (R2018a) 06-Feb-2018
 * C/C++ source code generated on : Wed Mar 23 18:43:39 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "stm32f4discovery_cmsis_crl.h"
#include "stm32f4discovery_cmsis_crl_private.h"

/* Block signals (default storage) */
B_stm32f4discovery_cmsis_crl_T stm32f4discovery_cmsis_crl_B;

/* Block states (default storage) */
DW_stm32f4discovery_cmsis_crl_T stm32f4discovery_cmsis_crl_DW;

/* Real-time model */
RT_MODEL_stm32f4discovery_cms_T stm32f4discovery_cmsis_crl_M_;
RT_MODEL_stm32f4discovery_cms_T *const stm32f4discovery_cmsis_crl_M =
  &stm32f4discovery_cmsis_crl_M_;
void RandSrc_GZ_R(real32_T y[], const real32_T mean[], int32_T meanLen, const
                  real32_T xstd[], int32_T xstdLen, uint32_T state[], int32_T
                  nChans, int32_T nSamps)
{
  int32_T i;
  int32_T j;
  real32_T r;
  real32_T x;
  real32_T s;
  real32_T y_0;
  int32_T chan;
  uint32_T icng;
  uint32_T jsr;
  int32_T samp;
  static const real32_T vt[65] = { 0.340945F, 0.45731461F, 0.539779305F,
    0.606242716F, 0.663169086F, 0.713697493F, 0.759612501F, 0.80203563F,
    0.841722727F, 0.879210174F, 0.914894819F, 0.949079096F, 0.98200053F,
    1.01384926F, 1.04478097F, 1.07492542F, 1.10439169F, 1.13327384F, 1.16165304F,
    1.18960094F, 1.21718144F, 1.24445164F, 1.27146351F, 1.29826498F, 1.32490075F,
    1.35141253F, 1.37783992F, 1.40422106F, 1.43059289F, 1.45699155F, 1.48345268F,
    1.51001215F, 1.53670609F, 1.56357121F, 1.59064543F, 1.61796796F, 1.64558017F,
    1.67352545F, 1.7018503F, 1.73060453F, 1.75984216F, 1.78962231F, 1.82001F,
    1.85107696F, 1.88290441F, 1.91558313F, 1.9492166F, 1.98392391F, 2.0198431F,
    2.05713558F, 2.09599304F, 2.13664508F, 2.17937136F, 2.22451758F, 2.27251863F,
    2.32393384F, 2.37950087F, 2.44022179F, 2.50751162F, 2.58346581F, 2.67139149F,
    2.77699423F, 2.77699423F, 2.77699423F, 2.77699423F };

  /* S-Function (sdsprandsrc2): '<Root>/Random Source' */
  /* RandSrc_GZ_R */
  for (chan = 0; chan < nChans; chan++) {
    icng = state[chan << 1];
    jsr = state[(chan << 1) + 1];
    for (samp = 0; samp < nSamps; samp++) {
      icng = 69069U * icng + 1234567U;
      jsr ^= jsr << 13;
      jsr ^= jsr >> 17;
      jsr ^= jsr << 5;
      i = (int32_T)(icng + jsr);
      j = (i & 63) + 1;
      r = (real32_T)i * 4.65661287E-10F * vt[j];
      if (!((real32_T)fabs(r) <= vt[j - 1])) {
        x = ((real32_T)fabs(r) - vt[j - 1]) / (vt[j] - vt[j - 1]);
        icng = 69069U * icng + 1234567U;
        jsr ^= jsr << 13;
        jsr ^= jsr >> 17;
        jsr ^= jsr << 5;
        y_0 = (real32_T)(int32_T)(icng + jsr) * 2.32830644E-10F + 0.5F;
        s = x + y_0;
        if (s > 1.30119801F) {
          r = r < 0.0F ? 0.487899214F * x - 0.487899214F : 0.487899214F -
            0.487899214F * x;
        } else {
          if (!(s <= 0.96892792F)) {
            x = 0.487899214F - 0.487899214F * x;
            if (y_0 > 12.6770601F - (real32_T)exp(-0.5F * x * x) * 12.3758602F)
            {
              r = r < 0.0F ? -x : x;
            } else {
              if (!((real32_T)exp(-0.5F * vt[j] * vt[j]) + y_0 * 0.0195830297F /
                    vt[j] <= (real32_T)exp(-0.5F * r * r))) {
                do {
                  icng = 69069U * icng + 1234567U;
                  jsr ^= jsr << 13;
                  jsr ^= jsr >> 17;
                  jsr ^= jsr << 5;
                  x = (real32_T)log((real32_T)(int32_T)(icng + jsr) *
                                    2.32830644E-10F + 0.5F) / 2.77699399F;
                  icng = 69069U * icng + 1234567U;
                  jsr ^= jsr << 13;
                  jsr ^= jsr >> 17;
                  jsr ^= jsr << 5;
                } while ((real32_T)log((real32_T)(int32_T)(icng + jsr) *
                                       2.32830644E-10F + 0.5F) * -2.0F <= x * x);

                r = r < 0.0F ? x - 2.77699399F : 2.77699399F - x;
              }
            }
          }
        }
      }

      y[chan * nSamps + samp] = xstd[xstdLen > 1 ? chan : 0] * r + mean[meanLen >
        1 ? chan : 0];
    }

    state[chan << 1] = icng;
    state[(chan << 1) + 1] = jsr;
  }

  /* End of S-Function (sdsprandsrc2): '<Root>/Random Source' */
}

void RandSrcInitState_GZ(const uint32_T seed[], uint32_T state[], int32_T nChans)
{
  int32_T i;

  /* InitializeConditions for S-Function (sdsprandsrc2): '<Root>/Random Source' */
  /* RandSrcInitState_GZ */
  for (i = 0; i < nChans; i++) {
    state[i << 1] = 362436069U;
    state[(i << 1) + 1] = seed[i] == 0U ? 521288629U : seed[i];
  }

  /* End of InitializeConditions for S-Function (sdsprandsrc2): '<Root>/Random Source' */
}

/* Model step function */
void stm32f4discovery_cmsis_crl_step(void)
{
  real32_T updateVal;
  int32_T j;

  /* S-Function (sdspsine2): '<Root>/Sine Wave' */
  updateVal = stm32f4discovery_cmsis_crl_P.SineWave_Frequency * 0.000785398181F;
  for (j = 0; j < 75; j++) {
    /* Update accumulated normalized freq value
       for next sample.  Keep in range [0 2*pi) */
    stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm += updateVal;
    if (stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm >= 6.28318548F) {
      stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm -= 6.28318548F;
    } else {
      if (stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm < 0.0F) {
        stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm += 6.28318548F;
      }
    }
  }

  /* End of S-Function (sdspsine2): '<Root>/Sine Wave' */

  /* S-Function (sdspsine2): '<Root>/Sine Wave1' */
  updateVal = stm32f4discovery_cmsis_crl_P.SineWave1_Frequency * 0.000785398181F;
  for (j = 0; j < 75; j++) {
    /* Update accumulated normalized freq value
       for next sample.  Keep in range [0 2*pi) */
    stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm += updateVal;
    if (stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm >= 6.28318548F) {
      stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm -= 6.28318548F;
    } else {
      if (stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm < 0.0F) {
        stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm += 6.28318548F;
      }
    }
  }

  /* End of S-Function (sdspsine2): '<Root>/Sine Wave1' */

  /* S-Function (sdsprandsrc2): '<Root>/Random Source' */
  RandSrc_GZ_R(stm32f4discovery_cmsis_crl_B.RandomSource,
               &stm32f4discovery_cmsis_crl_P.RandomSource_MeanVal, 1,
               &stm32f4discovery_cmsis_crl_P.RandomSource_VarianceRTP, 1,
               stm32f4discovery_cmsis_crl_DW.RandomSource_STATE_DWORK, 1, 75);
}

/* Model initialize function */
void stm32f4discovery_cmsis_crl_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(stm32f4discovery_cmsis_crl_M, (NULL));

  /* states (dwork) */
  (void) memset((void *)&stm32f4discovery_cmsis_crl_DW, 0,
                sizeof(DW_stm32f4discovery_cmsis_crl_T));

  {
    real32_T arg;

    /* Start for S-Function (sdspsine2): '<Root>/Sine Wave' */
    /* Trigonometric mode: compute accumulated
       normalized trig fcn argument for each channel */
    /* Keep normalized value in range [0 2*pi) */
    arg = (real32_T)fmod(stm32f4discovery_cmsis_crl_P.SineWave_Phase,
                         6.2831854820251465);
    if (arg < 0.0F) {
      arg += 6.28318548F;
    }

    stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm = arg;

    /* End of Start for S-Function (sdspsine2): '<Root>/Sine Wave' */

    /* Start for S-Function (sdspsine2): '<Root>/Sine Wave1' */
    /* Trigonometric mode: compute accumulated
       normalized trig fcn argument for each channel */
    /* Keep normalized value in range [0 2*pi) */
    arg = (real32_T)fmod(stm32f4discovery_cmsis_crl_P.SineWave1_Phase,
                         6.2831854820251465);
    if (arg < 0.0F) {
      arg += 6.28318548F;
    }

    stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm = arg;

    /* End of Start for S-Function (sdspsine2): '<Root>/Sine Wave1' */

    /* InitializeConditions for S-Function (sdspsine2): '<Root>/Sine Wave' */
    /* This code only executes when block is re-enabled in an
       enabled subsystem when the enabled subsystem states on
       re-enabling are set to 'Reset' */
    /* Reset to time zero on re-enable */
    /* Trigonometric mode: compute accumulated
       normalized trig fcn argument for each channel */
    /* Keep normalized value in range [0 2*pi) */
    arg = (real32_T)fmod(stm32f4discovery_cmsis_crl_P.SineWave_Phase,
                         6.2831854820251465);
    if (arg < 0.0F) {
      arg += 6.28318548F;
    }

    stm32f4discovery_cmsis_crl_DW.SineWave_AccFreqNorm = arg;

    /* End of InitializeConditions for S-Function (sdspsine2): '<Root>/Sine Wave' */

    /* InitializeConditions for S-Function (sdspsine2): '<Root>/Sine Wave1' */
    /* This code only executes when block is re-enabled in an
       enabled subsystem when the enabled subsystem states on
       re-enabling are set to 'Reset' */
    /* Reset to time zero on re-enable */
    /* Trigonometric mode: compute accumulated
       normalized trig fcn argument for each channel */
    /* Keep normalized value in range [0 2*pi) */
    arg = (real32_T)fmod(stm32f4discovery_cmsis_crl_P.SineWave1_Phase,
                         6.2831854820251465);
    if (arg < 0.0F) {
      arg += 6.28318548F;
    }

    stm32f4discovery_cmsis_crl_DW.SineWave1_AccFreqNorm = arg;

    /* End of InitializeConditions for S-Function (sdspsine2): '<Root>/Sine Wave1' */

    /* InitializeConditions for S-Function (sdsprandsrc2): '<Root>/Random Source' */
    RandSrcInitState_GZ(&stm32f4discovery_cmsis_crl_P.RandomSource_rawSeed,
                        stm32f4discovery_cmsis_crl_DW.RandomSource_STATE_DWORK,
                        1);
  }
}

/* Model terminate function */
void stm32f4discovery_cmsis_crl_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */



回复

使用道具 举报

2

主题

9

回帖

15

积分

新手上路

积分
15
发表于 2022-3-24 08:06:31 | 显示全部楼层
生成的代码可读性这么差呢?不科学啊
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106934
QQ
 楼主| 发表于 2022-3-24 09:43:45 | 显示全部楼层
laomao01211 发表于 2022-3-24 08:06
生成的代码可读性这么差呢?不科学啊

Simulink生成的代码确实不适合读,适合直接使用。
回复

使用道具 举报

0

主题

20

回帖

20

积分

新手上路

积分
20
发表于 2022-3-24 12:20:47 | 显示全部楼层
我司产品上也在用,嵌入式做底层,Simulink 做核心逻辑(或调PID之类很便捷),一般生成的代码就是这种风格,变量名臭长,形参巨多,堆栈占用多,仿佛目的就是不让人看懂,一般我们都不去读,盲盒测试功能是否实现即可 luoc3_2022-03-24_093413.png
回复

使用道具 举报

13

主题

192

回帖

231

积分

高级会员

积分
231
发表于 2022-3-24 14:13:01 | 显示全部楼层
你都看懂了,自己都会写了,还会用Simulink吗?所以,就是为了绑定用户
回复

使用道具 举报

5

主题

519

回帖

534

积分

金牌会员

积分
534
发表于 2022-3-24 14:41:22 | 显示全部楼层
x.or@qq.com 发表于 2022-3-24 12:20
我司产品上也在用,嵌入式做底层,Simulink 做核心逻辑(或调PID之类很便捷),一般生成的代码就是这种风格, ...

这参数看起来吓人,十来个参数。。。
回复

使用道具 举报

0

主题

10

回帖

10

积分

新手上路

积分
10
发表于 2022-3-27 07:11:12 | 显示全部楼层
可以自定义自动生成代码的标识和命名规则,有修改语法规则的……
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106934
QQ
 楼主| 发表于 2022-3-27 10:13:41 | 显示全部楼层
nicole088 发表于 2022-3-27 07:11
可以自定义自动生成代码的标识和命名规则,有修改语法规则的……

谢谢,函数命名倒是小事,主要不好魔改到现有工程里面,发现Matlab的m脚本生成C算法文件更适合我们玩。移植调用都贼方便。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-10 17:45 , Processed in 0.180601 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表