我正在尝试实施此
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
正如你所看到的,我没有得到这样的东西。
答案 0 :(得分:0)
我今天没有多少时间,所以我为普通计算机而不是嵌入式系统(例如Arduino,我认为你正在使用类似的东西)写了答案。
我认为你的增量是错误的,代码本身非常混乱所以我决定从头开始重写它(for循环中的错误,一个被严重评论的错误循环,错误的缩进......):
/** @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中。