%18-bit, 16-bit fraction, 2's comp conversion clear all figure(1);clf; clc %The scale has to be adjusted so that filter coefficients are % -1.0=0 fprintf(' .b%1d (18''h%s), \n', i, dec2hex(fix(2^16*b(i)))) ; else fprintf(' .b%1d (18''h%s), \n', i, dec2hex(bitcmp(fix(2^16*-b(i)),18)+1)); end end for i=2:length(a) if a(i)>=0 fprintf(' .a%1d (18''h%s), \n', i, dec2hex(fix(2^16*a(i)))) else fprintf(' .a%1d (18''h%s), \n', i,dec2hex(bitcmp(fix(2^16*-a(i)),18)+1)) end end fprintf(' .state_clk(AUD_CTRL_CLK), \n'); fprintf(' .lr_clk(AUD_DACLRCK), \n'); fprintf(' .reset(reset) \n'); fprintf(') ; //end filter \n'); disp(' ') disp('CHECK scaling! all b''s and a''s <1 absolute value?') disp('BUT as big as possible?') b a %sampling rate on DE2 board Fs = 48000; [b,a] = butter(order, freq) ; [fresponse, ffreq] = freqz(b,a,300); plot(ffreq/pi*Fs/2,abs(fresponse), 'b', 'linewidth',2); xlabel('frequency'); ylabel('filter amplitude'); hold on b = fix((b*(2^-scale))*2^16) ; a = fix((a*(2^-scale))*2^16) ; [fresponse, ffreq] = freqz(b,a,300); plot(ffreq/pi*Fs/2,abs(fresponse), 'r', 'linewidth',2); legend('exact','scaled 16-bit')