Some Problem
warning #10210-D: creating “.stack” section with default size of 0x400; use the -stack option to change the default size
warning #10210-D: creating “.sysmem” section with default size of 0x400; use the -heap option to change the default size
编译器默认创建的堆栈(stack)和堆(heap)大小是0x400即1024字节,当程序有大量局部变量,递归调用,或者需要动态分配大量内存的时候,需要将stack和heap调整到更大的空间
在CS6中更改stack 和 heap
在ccs中出现如上警告,且在运行后,printf打印的字符不完整且乱码时,是因为堆栈太小,修改堆栈的大小即可解决。在cmd文件中堆栈默认为0x400,本此在cmd中添加
-stack 0x1000
-heap 0x1000
c语言编写函数,完成点积运算
#include <stdio.h>
// 计算两个向量的点积
// 参数v1 v2 是输入向量的指针
double dotProduct(const float* v1,const float* v2,int size)
{
double sum = 0.0;
int i=0;
for(i=0;i<size;i++)
{
sum +=v1[i]*v2[i];
}
return sum;
}
/*
* main.c
*/
int main(void) {
printf("Hello World!\n");
float v1[] = {1,2,3,4};
float v2[] = {2,2,2,2};
double sum = dotProduct(v1,v2,4);
printf("sum is %f\n",sum);
return 0;
}
第二次作业-FIR滤波器设计
从matlab 中导入dat数据代码
#include <stdio.h>
#include "fdacoefs.h"
#define N 15 // 滤波器阶数
#define SIGNAL_SIZE 1024
#define address 0xc0000000
double filter_buffer[N] = {0}; // 循环缓冲区用于存储最近的N个输入样本
double fir_filter(unsigned int input) {
double output = 0;
int i;
// 循环缓冲区更新
for (i = N - 1; i >= 0; i--) {
if (i == 0) {
filter_buffer[i] = input; // 新的输入样本
} else {
filter_buffer[i] = filter_buffer[i - 1]; // 向后移动数据
}
}
// FIR滤波器计算
for (i = 0; i < N; i++) {
output += B[i] * filter_buffer[i]; // 卷积计算
}
return output;
}
int main(void) {
printf("Hello World!\n");
int i = 0;
unsigned int *c0 = (unsigned int *) 0xc0000000;
unsigned int new_sample[SIGNAL_SIZE]=0;
double filtered_sample[SIGNAL_SIZE];
for(; i < SIGNAL_SIZE; i++) { //i已经声明过,所以在for循环中省略初始化过的变量
new_sample[i] = *(c0 + i);
// printf("%u\n", new_sample[i]);
filtered_sample[i] = fir_filter(new_sample[i]);
}
// 现在 filtered_sample 数组包含了处理过的样本
printf("compute ok");
return 0;
}
参考资料
http://www.360doc.com/content/18/0301/08/44422250_733342963.shtml
https://blog.csdn.net/weixin_46774073/article/details/114926977
由CCS自己产生信号
需要注意的是,v6版本CCS 在输出信号定义为long ,也即32位整型时,绘制函数曲线会报错,因此需要将变量设置为float格式
#include "stdio.h"
#include "fdacoefs.h"
#include <math.h>
#include <stdlib.h>
#define Length 1024
#define pi 3.1415926
int fs=5000; //采样频率
int f1=50; //信号频率
int f2=500; //噪声频率
float res; //保存滤波后结果,32位长整型
float input[Length]; //
float output[Length];
int i;
void main() {
int m,n;
for(i=0;i<Length;i++)
input[i]=1024*sin(2*pi*f1/fs*i)+1024*sin(2*pi*f2/fs*i);
for(n=0;n<Length+BL;n++) //与滤波器卷积
{
res=0;
for(m=0;(m<BL)&&(m<n);m++)
res+=B[m]*input[n-m];
output[n]=res;
}
while(1);
}
参考资料
https://blog.csdn.net/SSG18829575503/article/details/80971003?utm_source=miniapp_weixin
matlab信号仿真 滤波代码
% 定义参数
fs = 5000; % 采样频率,单位:Hz
t = 0:1/fs:(1024-1)/fs; % 时间向量
f = 50; % 正弦波频率,单位:Hz
% 生成正弦波
sinewave = sin(2 * pi * f * t);
fft_sinewave = fft(sinewave,1024);
% 添加高频噪声
% 噪声水平可以调整
noise_wave = sin(2*pi*10*f*t);
noisy_signal = sinewave + noise_wave;
fft_noisy_signale = fft(noisy_signal,1024);
% 绘制结果
figure;
subplot(5,1,1);
plot(t, sinewave);
title('原始正弦波 频率50Hz');
subplot(5,1,2);
plot(t, noisy_signal);
title('含噪声的正弦波 噪声频率500Hz');
subplot(5,1,3);
plot(t(1:512), abs(fft_noisy_signale(1:512)));
title('含噪声信号傅里叶频谱');
%首先将x强制转换成single型,然后再转换成具有相同二进制形式的uint32型
uint32_noisy_signal = typecast(single(noisy_signal),'uint32');
signle_noisy_signal = single(noisy_signal);
fid = fopen('signle_noisy_signal.dat','wt');
disp(length(t))
fprintf(fid,'1651 1 c0000000 0 %x\n',length(t));
% fprintf(fid,'0x%08x\n',noisy_signal);
fprintf(fid,'0x%08x\n',signle_noisy_signal);
fclose(fid);
filtered_signal = filter(Num, 1, noisy_signal);
fft_filtered_signal = fft(filtered_signal,1024);
subplot(5,1,4);
plot(t, filtered_signal);
title('滤波后的正弦波');
subplot(5,1,5);
plot(t(1:512), abs(fft_filtered_signal(1:512)));
title('滤波后信号的傅里叶频谱');
第三次作业
要求
matlab仿真代码
% close all;
% clear all;
% clc;
% 定义参数
fs = 5000; % 采样频率,单位:Hz
t = 0:1/fs:(1024-1)/fs; % 时间向量
f = 50; % 正弦波频率,单位:Hz
x = zeros(1,16); %定义一个16位大小的数组,
FIR_signal = zeros(1,1024);
% 生成正弦波
sinewave = sin(2 * pi * f * t);
fft_sinewave = fft(sinewave,1024);
% 添加高频噪声
% 噪声水平可以调整
noise_wave = sin(2*pi*10*f*t); %噪声频率是信号频率的10倍
noisy_signal = sinewave + noise_wave;
fft_noisy_signale = fft(noisy_signal,1024);
%%%%%%%%%%%%% 将数据写入 dtb文件
%首先将x强制转换成single型,然后再转换成具有相同二进制形式的uint32型
uint32_noisy_signal = typecast(single(noisy_signal),'uint32');
% signle_noisy_signal = single(noisy_signal);
fid = fopen('noisy_signal.dat','wt');
disp(length(t))
fprintf(fid,'1651 1 c0000000 0 %x\n',length(t));
fprintf(fid,'0x%08x\n',uint32_noisy_signal);
fclose(fid);
% 绘制结果
figure;
subplot(5,1,1);
plot(t, sinewave);
title('原始正弦波 频率50Hz');
subplot(5,1,2);
plot(t, noisy_signal);
title('含噪声的正弦波 噪声频率500Hz');
subplot(5,1,3);
plot(t(1:512), abs(fft_noisy_signale(1:512)));
title('含噪声信号傅里叶频谱');
index = 1;
noisy_signal(1025:1040) = 0;
for i=1:1024
x = noisy_signal(i:i+15);
FIR_signal(index) = dot(x,FIR);
index =index+1;
end
subplot(5,1,4);
plot(t, FIR_signal);
title('滤波后的信号');
matlab读取.dat文件代码
% 假设.dtb文件包含32位整数数据
filename = 'output.dat'; % .dtb文件的路径
% 打开文件进行读取,'rb'模式表示以二进制形式读取
fileID = fopen(filename, 'r');
read_data = fscanf(fileID,'%x\n');
% 关闭文件
fclose(fileID);
data = read_data(7:end);
z = typecast(uint32(data),'single');
figure;
plot(z);
CCS Load Memory 滤波代码
#include <stdio.h>
#include "fdacoefs.h"
#include <string.h>
#define N 15 // 滤波器阶数
#define SIGNAL_SIZE 1024
#define address 0xc0000000
int i,n,index; //定义全局变量
// 定义导入数据载入变量
float input_sample[SIGNAL_SIZE]={0.0};
// 定义滤波结果输出变量
float output[SIGNAL_SIZE];
// 定义一个16位的数组
float x[16]={0.0};
// 点积运算 用于滤波操作
double dotProduct(const float* v1,const float* v2,int size)
{
double sum = 0.0;
int i=0;
for(i=0;i<size;i++)
{
sum +=v1[i]*v2[i];
}
return sum;
}
int main(void) {
//定义导入数据的内存地址
volatile unsigned int* ptr = (volatile unsigned int*)0xC0000000;
for(i=0; i < SIGNAL_SIZE; i++) {
// 读取该地址中的数据
unsigned int data = *(ptr+i);
float* float_ptr = (float*)&data; //把数据转为32位浮点型
input_sample[i] = *float_ptr;
}
index=0;
for(n=0;n<SIGNAL_SIZE-BL;n++)
{
memcpy(x,input_sample+n,16*4); //从输入数据中依次取出16位数据
output[index]=dotProduct(x,B,16); //点积运算 得到结果
index =index+1;
}
printf("compute ok");
return 0;
}