【问题标题】:Matlab to C implementing the for loopMatlab 到 C 实现 for 循环
【发布时间】:2018-09-21 07:29:50
【问题描述】:

我正在尝试实现这个

Fs = 2000;            % Sampling frequency                    
T = 1/Fs;             % Sampling period       
L = 4000;             % Length of signal
t = (0:L-1)*T;          %time samples
A=[16 18 28 33 38 41 59 43 40 58];% Amplitude of noise in dBA (refer to the measurements from consultant)
forigin=[25 31.5 40 50 63 80 100 125 160 200];% Frequency of the noise components (refer to the measurements from consultant)
S=zeros(1,length(t));% This will be the signal representing transformer noise, in your case, it will be the sound created from exciter 
%V=zeros(1,L);%not needed, you can remove this
for k=1:length(A)
S = S+10^(A(k)/20)*sin(2*pi*forigin(k)*t);%Creating the transformer noise from the amplitude and frequency components, in your case, it will be the sound created from exciter

end

到目前为止,我已经得到了这个,似乎 k 没有通过 forigin 数组递增。

#define _USE_MATH_DEFINES  // to force M_PI to exist
#include <math.h>          //for pow, sin and M_PI
#define ASIZE 10   //for A 
#define TSIZE 40   //or 4000
int Fs = 2000;
int L = 40;
double A[ASIZE] = {16, 18, 28, 33, 38, 41, 59, 43, 40, 58};
double forigin[ASIZE] = {25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0};
double S[TSIZE];
double t[TSIZE];
double T = 1/(double)Fs;
int i,k,  j;
//int k = 10;
 double  apow = pow(10, A[k]/20);
void setup() {

  Serial.begin(9600);
}

void loop()
{
   for ( i = 0; i < TSIZE ; i++ )  /* initialize elements of array t to 0 */  
       {
      t[i] = (double) i*T; /* set element at location i to i*T */
       }
      for (j = 0; j < TSIZE; j++ ) /* output each array element's value */
       {

//      Serial.print(t[j],15);
//     Serial.println("");
//     delay(500);
       } 

//initialise S to 0
for (i = 0; i < TSIZE; ++i) //S[i] = 0;

//implement the for k=1:length(A)

  for (k = 0; i < ASIZE; ++k){
   //devectorise the S calculation
   for (i = 0; i < TSIZE; ++i){
       S[i] = S[i] + pow(10, A[k]/20) * sin(2*M_PI*forigin[k]*t[i]);
       Serial.print(S[i]);
     Serial.println("");
     delay(500);

有人可以帮忙吗? Matlab已经推出了

matlab中的实际S值是 0 899.475071494380 1570.91017541897 1861.36356724185 1740.88440906200 1306.16803613629 739.441548916790 238.639601470524 -54.6370978848435 -102.514143930475

如你所见,我没有得到这样的东西。

【问题讨论】:

    标签: arrays matlab for-loop multidimensional-array


    【解决方案1】:

    我今天没有太多时间,所以我为普通计算机而不是嵌入式系统(例如Arduino,我认为您正在使用类似的东西)写了一个答案。

    我认为你的增量错误,而且代码本身很混乱,所以我决定从头开始重写它(for 循环中的错误,一个被错误注释的 forr 循环,糟糕的缩进......):

    /** @file: test.c */
    #include <stdio.h>
    #include <math.h>
    #include <stddef.h>
    
    #define T_LENGTH 4000
    #define A_LENGTH 10
    
    int main() {
      const double Fs = 2000.0;
      const double T = 1/Fs;
    
      const double A[A_LENGTH] = {
        16, 18, 28, 33, 38, 41, 59, 43, 40, 58
      };
    
      const double forigin[A_LENGTH] = {
        25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200
      };
    
      double t[T_LENGTH];
      double S[T_LENGTH] = {0};
    
      for  (size_t i = 0; i < T_LENGTH; i++) 
        t[i] = (double)(i) * T;
    
      for (size_t i = 0; i < A_LENGTH; i++) { // it is i++ not ++i
        for (size_t j = 0; j < T_LENGTH; j++) {
          S[j] += pow(10, A[i] / 20) * sin(2 * M_PI * forigin[i] * t[j]);
        }
      }
    
      // Printing out the results
      for (size_t j = 0; j < T_LENGTH; j++)
        printf("%f\n", S[j]);
    
      return 0;
    }
    

    您可以使用以下代码编译此代码:

    gcc test.c -lm -o test
    

    然后运行它:

    ./test > output.txt
    

    将输出放在一个文件中并与您在 Matlab 上的结果进行比较:

    load output.txt -ascii
    norm(S' - output)
    % something in the order of 1e-7
    

    检查一切正常后,您可以将其放入 Arduino IDE。

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 1970-01-01
      • 2017-08-30
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多