当前位置: 首页 > news >正文

Matlab dsp工具箱可以实现定点FFT的功能

但是要主意输入信号的定点格式 ,正弦信号用Q64.64 ,也就是字长128,小数位数64,得到的结果完美。
但是要是Q2.14则是无意义的结果。
代码:

点击查看代码
%% 生成sin信号并使用定点FFT计算频谱 - 带错误诊断
clear; clc; close all;%% 参数设置
Fs = 1000;           % 采样频率 1kHz
f0 = 50;             % 信号频率 50Hz
A = 1;               % 信号幅度
N = 256;             % FFT点数
t = (0:N-1) / Fs;    % 时间向量% 定点参数
wordLength = 128 ;%16;     % 总字长
fractionLength = 64;%14; % 小数位长度fprintf('信号参数: %.1fHz正弦波, 采样率%.1fHz, FFT点数%d\n', f0, Fs, N);
fprintf('定点参数: %d位, 小数位%d位\n', wordLength, fractionLength);%% 生成sin信号
signal_float = A * sin(2*pi*f0*t);% 创建定点信号
signal_fi = fi(signal_float, 1, wordLength, fractionLength);fprintf('信号范围: [%.6f, %.6f]\n', min(signal_fi), max(signal_fi));%% 配置定点FFT - 带详细错误诊断
tryfprintf('\n=== 开始配置定点FFT ===\n');fft_fixed = dsp.FFT();fprintf('✓ dsp.FFT对象创建成功\n');fft_fixed.FFTLengthSource="Property" ;fft_fixed.FFTLength = N;fprintf('✓ FFTLength设置成功\n');nt_sine = numerictype('WordLength', wordLength, 'FractionLength', fractionLength, 'Signedness', 'Auto'); nt_sine.Scaling = 'Unspecified';nt_product = numerictype('WordLength', wordLength, 'FractionLength', fractionLength, 'Signedness', 'Auto');nt_accumulator = numerictype('WordLength', wordLength, 'FractionLength', fractionLength, 'Signedness', 'Auto');nt_output = numerictype('WordLength', wordLength, 'FractionLength', fractionLength, 'Signedness', 'Auto');properties_to_set = {{'RoundingMethod', 'Nearest'},{'OverflowAction', 'Saturate'},{'SineTableDataType', 'Custom'},{'CustomSineTableDataType', nt_sine} ,{'ProductDataType', 'Custom'},{'CustomProductDataType', nt_product},{'AccumulatorDataType', 'Custom'},{'CustomAccumulatorDataType', nt_accumulator},{'OutputDataType', 'Custom'},{'CustomOutputDataType', nt_output}};for i = 1:length(properties_to_set)prop_name = properties_to_set{i}{1};prop_value = properties_to_set{i}{2};tryif ischar(prop_value)set(fft_fixed, prop_name, prop_value);elseset(fft_fixed, prop_name, prop_value);endfprintf('✓ %s 设置成功\n', prop_name);catch MEfprintf('❌ %s 设置失败:\n', prop_name);fprintf('   错误信息: %s\n', ME.message);fprintf('   错误标识符: %s\n', ME.identifier);fprintf('   属性值类型: %s\n', class(prop_value));if isnumeric(prop_value) || isa(prop_value, 'embedded.numerictype')fprintf('   属性值: %s\n', char(prop_value));end% 显示属性信息fprintf('   属性信息:\n');meta_info = findprop(fft_fixed, prop_name);if ~isempty(meta_info)fprintf('     类型: %s\n', meta_info.DataType);if ~isempty(meta_info.Description)fprintf('     描述: %s\n', meta_info.Description);endendrethrow(ME);endendfprintf('\n✓ 所有定点FFT配置完成\n');%% 执行定点FFTfprintf('\n执行定点FFT计算...\n');fft_output_fixed = fft_fixed(signal_fi');fprintf('✓ 定点FFT计算成功\n');fft_output_float = double(fft_output_fixed);mag_fixed = abs(fft_output_float(1:N/2)) / (N/2);catch MEfprintf('\n❌ 定点FFT配置或计算失败!\n');fprintf('主要错误: %s\n', ME.message);fprintf('错误标识符: %s\n', ME.identifier);% 显示完整错误堆栈fprintf('\n错误堆栈:\n');for i = 1:length(ME.stack)fprintf('  %s (第%d行)\n', ME.stack(i).name, ME.stack(i).line);end% 尝试简化配置fprintf('\n=== 尝试简化配置 ===\n');tryfft_simple = dsp.FFT('FFTLength', N);fft_output_simple = fft_simple(signal_fi');mag_fixed = abs(fft_output_simple(1:N/2)) / (N/2);fprintf('✓ 简化配置成功 - 使用默认定点设置\n');catch ME2fprintf('简化配置也失败: %s\n', ME2.message);% 最后备选:使用浮点FFTfprintf('使用浮点FFT作为最后备选\n');fft_float_temp = fft(double(signal_fi));mag_fixed = abs(fft_float_temp(1:N/2)) / (N/2);end
end%% 浮点FFT作为参考
fft_float = fft(signal_float);
mag_float = abs(fft_float(1:N/2)) / (N/2);%% 频率轴
freq_axis = (0:N/2-1) * Fs / N;%% 计算误差
error_val = abs(mag_float - double(mag_fixed));%% 性能分析
[peak_float, idx_float] = max(mag_float);
freq_float = freq_axis(idx_float);
[peak_fixed, idx_fixed] = max(mag_fixed);
freq_fixed = freq_axis(idx_fixed);
max_error = max(error_val);
rms_error = rms(error_val);fprintf('\n性能对比:\n');
fprintf('浮点FFT: 峰值 %.4f @ %.1fHz\n', peak_float, freq_float);
fprintf('定点FFT: 峰值 %.4f @ %.1fHz\n', peak_fixed, freq_fixed);
fprintf('最大误差: %.6f, RMS误差: %.6f\n', max_error, rms_error);%% 绘制图形
figure('Position', [100, 100, 1400, 600]);% 子图1: 时域信号
subplot(1,3,1);
plot(t, signal_float, 'b-', 'LineWidth', 1.5);
hold on;
plot(t, double(signal_fi), 'ro', 'MarkerSize', 3, 'MarkerFaceColor', 'r');
xlabel('时间 (s)');
ylabel('幅度');
title('时域信号');
legend('浮点信号', '定点信号', 'Location', 'northeast');
grid on;% 子图2: 浮点FFT频谱
subplot(1,3,2);
stem(freq_axis, mag_float, 'b.', 'MarkerSize', 10, 'LineWidth', 1);
xlabel('频率 (Hz)');
ylabel('幅度');
title('浮点FFT频谱');
xlim([0, 200]); ylim([0, 1.2]);
grid on;
hold on;
plot(freq_float, peak_float, 'ro', 'MarkerSize', 10, 'LineWidth', 2);
text(freq_float, peak_float+0.15, sprintf('峰值: %.4f\n频率: %.1fHz', peak_float, freq_float), ...'HorizontalAlignment', 'center', 'FontWeight', 'bold', 'FontSize', 9);% 子图3: 定点FFT频谱
subplot(1,3,3);
stem(freq_axis, double(mag_fixed), 'r.', 'MarkerSize', 10, 'LineWidth', 1);
xlabel('频率 (Hz)');
ylabel('幅度');
title('定点FFT频谱');
xlim([0, 200]); ylim([0, 1.2]);
grid on;
hold on;
plot(freq_fixed, peak_fixed, 'bo', 'MarkerSize', 10, 'LineWidth', 2);
text(freq_fixed, peak_fixed+0.15, sprintf('峰值: %.4f\n频率: %.1fHz', peak_fixed, freq_fixed), ...'HorizontalAlignment', 'center', 'FontWeight', 'bold', 'FontSize', 9);% 总标题
sgtitle(sprintf('定点FFT频谱分析 (%.0fHz正弦信号, %d点FFT, %d位定点)', f0, N, wordLength), ...'FontSize', 14, 'FontWeight', 'bold');fprintf('\n图形已生成完成!\n');
![dspfix64.64fftvsfft](https://img2024.cnblogs.com/blog/3707231/202509/3707231-20250930163952509-167523278.jpg) 要是把其中定义定点格式的两个变量修改一下:
点击查看代码
% 定点参数
wordLength = 16;%128 ;%16;     % 总字长
fractionLength = 14;%64;%14; % 小数位长度
则频谱会如下图: ![dspfix2.14fftvsfft](https://img2024.cnblogs.com/blog/3707231/202509/3707231-20250930164451013-704167377.jpg)
http://www.sczhlp.com/news/151267/

相关文章:

  • MySQL悲观锁(排他锁)级别
  • Swagger 3.0 + Knife4j 入门到实战:Spring Boot API 文档搭建、注解详解与生产环境安装
  • 手机网站建设市场收费网站方案
  • 免费网站ps做网站教程
  • 庙行镇seo推广网站红安县建设局网站
  • 网站备案简介怎么写网站建设 技术 哪些
  • 网站运行速度慢建筑课堂首页
  • 做得好的网站电子商务平台建设流程6步骤
  • 怎样给网站登录界面做后台公众号怎么制作推文
  • 做网站第一次见客户绵阳公司网站建设
  • 浅谈京东企业的电子商务网站建设正规软件开发培训学校
  • 网站建设推广新业务云南餐饮网站建设
  • Learning Continuous Image Representation with Local Implicit Image Function
  • 北京天润建设工程有限公司网站动画设计学校
  • 公司网站宣传自己做的灯展在线制作离婚证图片
  • 租房网站开发视频教程做电商平台网站有哪些内容
  • 装修设计公司起名搜索引擎营销优化的方法
  • 波莱网站开发网页视频怎么下载插件
  • 做教育网站宣传策略wordpress插件 自动翻译
  • 网站建设学习内容网页设计模板html代码ie
  • 公司网站制作制作写作网站一稿多投的后果
  • 南宁做网站方案天气预报权威发布
  • 关于网站优化的文章手机网页在线游戏
  • 上海城市建设官方网站免费windows7云主机
  • cpa诱导网站怎么做网站展示怎么做
  • 想学网站搭建与推广游戏开发网站建设
  • 做试试彩网站什么叫门户类网站
  • 百度竞价一个月5000够吗医疗网站怎么做seo
  • 养老做增减的网站广州建站推广
  • 安卓手机怎么做网站重庆市建设造价信息网