⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fir_compare_script.m

📁 This getting started exercise will guide you through the step-by-step process of transforming a MATL
💻 M
字号:
%       AccelDSP 8.1.1 build 690 Production, compiled Apr 26 2006 
% 
%    THIS IS UNPUBLISHED, LICENSED SOFTWARE THAT IS THE CONFIDENTIAL 
%        AND PROPRIETARY PROPERTY OF XILINX OR ITS LICENSORS 
% 
%     Copyright(c) Xilinx, Inc., 2000-2006, All Rights Reserved. 
%     Reproduction or reuse, in any form, without the explicit written 
%     consent of Xilinx, Inc., is strictly prohibited. 

close all
% This global is used to to distinguish Verify -floatingpoint from -fixedpoint
global AccelVerifyFixedPoint; 

% Define constants
NUMTAPS = 16;    % Filter order
NUMSAMPS = 200;  % Number of input vectors
Fs = 44100;      % Sampling frequency in Hz
Fc = 2000;       % Filter cutoff frequency in Hz
SINEFREQ = 1000; % Input sine frequency in Hz

Wn = 2*Fc/Fs;

% To generate and save new coefficients, uncomment
% the following two commands.  Verify -floatingpoint
% must be re-run to update the coefficients before
% generating new hardware.
%
%coeff = FIR1(NUMTAPS-1,Wn,'low'); % Can be low or high
%save coefficients.txt coeff -ascii

% Create input data
data = 5 * sin( 2 * pi * [1:NUMSAMPS] / (Fs/SINEFREQ));
noise = 2*(rand(1,NUMSAMPS)-0.5);

indata = data + noise;

% Apply filter to each input sample
for n = 1:NUMSAMPS
    
  % Call function targeted to hardware
  outdata(n) = fir(indata(n));

end

% Distinguish Verify -fixedpoint from Verify -floating point
% Handle the case where Verify -floatingpoint was skipped
verify_floatingpoint_skipped = 0;
if ~isempty(AccelVerifyFixedPoint)
    % This is Verify -fixedpoint
	if exist('outdata.txt')
		outdata_float = load('outdata.txt');
	else
		verify_floatingpoint_skipped = 1;
	end
else
    % This is Verify -floatingpoint
	save 'outdata.txt' -ascii -double outdata
end

% Plot input signal components
figure(1);
subplot(2,1,1);
plot(data);
axis([1 NUMSAMPS -6 6]);
title(['Input = ',num2str(SINEFREQ),' Hz']);
subplot(2,1,2);
plot(noise);
axis([1 NUMSAMPS -6 6]);
title('Noise');

% Plot input and output of filter

if (~isempty(AccelVerifyFixedPoint) && verify_floatingpoint_skipped == 0)
% This is Verify -fixedpoint and Verify -floatingpoint has been run
	figure(2);
	% Top plot shows the Filter Input
	subplot(3,1,1);
	plot(indata);
	axis([1 NUMSAMPS -6 6]);
	title('Combined Input');
	% Middle plot shows the Filter Output
	subplot(3,1,2);
	hold off
	% Plot the fixed-point result in Blue
	plot(1:length(outdata),outdata, 'b');
	hold on
	% Overlay the floating-point result in Red
	plot(1:length(outdata),outdata_float,'r');
	hold off
	axis([1 NUMSAMPS -6 6]);
	title('Filtered Output');
	legend('Fixed-Point','Floating-Point');  
	% Lower plot shows the difference between Floating Point and Fixed Point
	subplot(3,1,3);
	% You can create fidelity checks with the absolute or relative error
	data_diff = outdata_float - outdata;
	[max_error, max_location] = max(abs(data_diff));
    plot(data_diff);
    axis([1 NUMSAMPS -6 6]);
    title_text = sprintf('Floating & Fixed Output difference\nLargest error = %d at sample %d',max_error, max_location);
    title(title_text);
else
	figure(2);
	% Top plot shows the Filter Input
	subplot(2,1,1);
	plot(indata);
	axis([1 NUMSAMPS -6 6]);
	title('Combined Input');
	% Lower plot shows the Filter Output
	subplot(2,1,2);
	plot(outdata);
	axis([1 NUMSAMPS -6 6]);
	title('Filtered Output');
end


% Plot PSD of input and output
figure(3);
[Pin,Hin]=aw_psd(indata,NUMSAMPS,Fs);
[Pout,Hout]=aw_psd(outdata,NUMSAMPS,Fs);
hold off
plot(Hin,10*log10(Pin))
hold on
plot(Hout,10*log10(Pout),'r')
grid on
title('Input and Output PSD');
legend('Input','Output');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -