Problem auto-scaling FIR coefficients

Hi,
I am having trouble creating a filter in MATLAB with auto-scaled coefficients. My code is below...
Hf = fdesign.lowpass('Fp,Fst,Ap,Ast',10,15,0.1,90,165);
% Design the above filter using equiripple optimization
% Filter structure is the polyphase fir decimator
Hd = design(Hf,'equiripple');
OutputZero = ADC_IR(1:Fs*t_stop_nl) / ADC_raw_count_value;
%LowRateFull = 4*filter(Hd,Output);
% Filter the amplifier output without quantization to get the best case
LowRateFull = filter(Hd,OutputZero);
figure;
plot(t_on_filt,1.8-LowRateFull(t_start_light*Fs:t_stop_light*Fs));
title('Ideal FIR LPF Filter');
% Now quantize the filter
% Hd.Arithmetic allows us to change to a quantized filter
Hd.Arithmetic = 'fixed';
% The filter accepts signed inputs
Hd.Signed = true;
% This allows us to change the number of bits for the multiplier
% and the accumulator, as well as the input and output word length
Hd.FilterInternals = 'specifyPrecision';
% Set the multiplication coefficients to be 16 bits
set(Hd, 'CoeffWordLength',16);
% Set the input word length to be 11 bits (signed)
Hd.InputWordLength = 11;
%Hd.InputFracLength = 9;
% Set the output word length to be 17 bits (signed)
Hd.OutputWordLength = 17;
%Hd.OutputFracLength = 16;
% Set the multiplier to 24 bits to be a conventional architecture
set(Hd,'ProductWordLength',24);
% Set the Accumulator word length to 28 for a conventional architecture
set(Hd,'AccumWordLength',28);
% If there is an overflow, saturate the data instead of wrapping
set(Hd,'OverflowMode','saturate');
% Now design an input to go from -1.8 to 1.8 so that we can
% set the dynamic range of the coefficients to be optimized
x = 1.8*sign(fliplr(impz(Hd)));
% Autoscales the filter coefficients to provide the best dynamic range
% Without causing overflows
%Hd = autoscale(Hd,x);
autoscale(Hd,x);
% Change MATLAB settings such that it will not try and change the
% datatype (forceoff) and it will log any overflows
% We can plot y to visually inspect the output of x
fipref('LoggingMode','on','DataTypeOverride','ForceOff');
y = filter(Hd,x);
fipref('LoggingMode','off');
% Display a report of overflows given input x
R = qreport(Hd)
% Plot the output with zeros attached for debugging
figure;
plot(OutputZero);
% Quantize the output to 11 signed bits
%Outputq = fi(Output,true,11,9);
Outputq = fi(OutputZero/512,true,11,9);
Outputq10 = fi(OutputZero, false, 10, 9);
% Send the quantized signal through the quantized filter
LowRate = filter(Hd,Outputq);
% original and quantized filter output
figure;
subplot(2,1,1);
plot(1.8-Outputq);
subplot(2,1,2);
plot(double(LowRate-LowRateFull));
% Plot the difference between the quantized and original amplifier output
figure;
plot(double(OutputZero - Outputq));
% Decimate the signal
%LowRate = downsample(filtOut(3:length(filtOut)),4);
%LowRatespec = fft(LowRate,10*length(LowRate));
%HzLowRate = 0:LEDSampleFreq/length(LowRatespec):LEDSampleFreq-1/length(LowRatespec);
%figure;
%freqz(B,1, 512, 660);
%set(gca,'FontSize',16);
%title('Frequency Spectrum of BPF Centered at 180 Hz','FontSize',20);
%xlabel('FontSize',16);ylabel('FontSize',16);
LowRate = cast(LowRate,'double');
% Plot the quantized filtered output
figure;
subplot(2,1,1);
plot(t_on_filt,1.8-LowRate(1:length(t_on_filt)));
set(gca,'FontSize',16);
title('Filtered Output','FontSize',20);
xlabel('Time [s]','FontSize',16);ylabel('Voltage [V]','FontSize',16);
subplot(2,1,2);
signalspec2 = fft(LowRate,10*length(LowRate));
subplot(2,1,2);
plot(HzLEDSampleFreq(1:length(HzLEDSampleFreq)/2),20*log10(abs(signalspec2(1:length(HzLEDSampleFreq)/2))));
set(gca,'FontSize',16);
title('Frequency Spectrum of Output','FontSize',20);
xlabel('Frequency [Hz]','FontSize',16);ylabel('Decibels [dB]','FontSize',16);
% Display information about the quantized filter
measure(Hd) % General filter characteristics
cost(Hd) % Number of mults and additions per input sample
info(Hd) % Quantization and filter structure information
fvtool(Hd) % Frequency response plot of the quantized filter
Whenever the code gets to the autoscale line, I get the error:
Error using subsindex
Function 'subsindex' is not defined for values of class 'dfilt.dffir'.
Error in Data_postanal (line 152)
autoscale(Hd,x);
When I looked up the error it seemed like it is usually caused by variables having the same name as a function. However, I do not think there are any functions called Hd. Any help would be appreciated.
Thanks! -Eric

 Respuesta aceptada

Wayne King
Wayne King el 8 de Nov. de 2013
Clear your workspace and see if you can execute this code:
Hf = fdesign.lowpass('Fp,Fst,Ap,Ast',10,15,0.1,90,165);
Hd = design(Hf,'equiripple');
Hd.Arithmetic = 'fixed';
Hd.Signed = true;
Hd.FilterInternals = 'specifyPrecision';
set(Hd, 'CoeffWordLength',16);
Hd.InputWordLength = 11;
Hd.OutputWordLength = 17;
Hd.OutputFracLength = 16;
set(Hd,'ProductWordLength',24);
set(Hd,'AccumWordLength',28);
set(Hd,'OverflowMode','saturate');
x = 1.8*sign(fliplr(impz(Hd)));
Hdautoscaled = autoscale(Hd,x);
autoscale(Hd,x);

Más respuestas (2)

Eric
Eric el 8 de Nov. de 2013

0 votos

I can run that code after I clear my workspace

3 comentarios

Wayne King
Wayne King el 8 de Nov. de 2013
So perhaps you had a variable hanging around that messed things up?
Eric
Eric el 8 de Nov. de 2013
apparently I do... I just don't understand why a variable would cause autoscale to not work?
Eric
Eric el 8 de Nov. de 2013
I still can't get autoscale to work even if I change the names of Hd and x...

Iniciar sesión para comentar.

Wayne King
Wayne King el 8 de Nov. de 2013

0 votos

Can you please attach the data, or whatever else is need to actually make this run. For example, we don't know what ADC_IR() is, or the values of Fs, t_stop_n1, etc.

1 comentario

Eric
Eric el 8 de Nov. de 2013
I just found the problem. There was a variable called autoscale in the dataset we were looking at. I deleted the variable and everything works now. Thanks for shedding light on the error!

Iniciar sesión para comentar.

Categorías

Más información sobre DSP System Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Nov. de 2013

Comentada:

el 8 de Nov. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by