Index exceeds the number of array elements (4)

8 visualizzazioni (ultimi 30 giorni)
Zach Fisher
Zach Fisher il 9 Ago 2020
Commentato: Fares Saleh il 29 Nov 2020
I keep running into the same error for my functions. The error and functions (uniquan.m and sampandquant.m) are shown below:
>> Exsample
Index exceeds the number of array elements (4).
Error in uniquan (line 19)
q_out=q_level(qindex); % use index vector to generate output
Error in sampandquant (line 21)
[sq_out,Delta,SQNR]=uniquan(s_out,L);
Error in Exsample (line 12)
[s_out,sq_out,sqh_out,Delta,SQNR]=sampandquant(xsig,16,td,ts);
Here is my function:
% (Exsample.m)
% Example of sampling, quantization and zero-order hold
clear;clf;
td=0.002; %origional sampling rate 500 Hz
t=[0:td:1.]; %time interval of 1 second
xsig=sin(2*pi*t)-sin(6*pi*t); % 1Hz+3Hz sinusoids
Lsig=length(xsig);
ts=0.02; %new sampling rate = 50Hz.
Nfactor=ts/td;
% send the signal through a 16-level uniform quantizer
[s_out,sq_out,sqh_out,Delta,SQNR]=sampandquant(xsig,16,td,ts);
% receive 3 signals
% 1. sampled signal s_out
% 2. sampled and quantized signal sq_out
% 3. sampled, quantized, and zero-order hold signal sqh_out
%
% Calculate the Fourier Transfoms
Lfft=2^ceil(log2(Lsig)+1);
Fmax=1/(2*td);
Faxis=linspace(-Fmax,Fmax,Lfft);
Xsig=fftshift(fft(xsig,Lfft));
S_out=fftshift(fft(s_out,Lfft));
And I'm drawing from:
% (uniquan.m)
function [q_out,Delta,SQNR]=uniquan(sig_in,L)
% Usage
% [q_out,Delta,SQNR]=uniquan(sig_in,L)
% L - number of uniform quantization levels
% sig_in - input signal vector
% Function outputs:
% q_out - quantized output
% Delta - quantization interval
% SQNR - actual signal to quantization noise ratio
sig_pmax=max(sig_in); % finding the positive peak
sig_nmax=min(sig_in); % finding the negative peak
Delta=(sig_pmax-sig_nmax)/L; % quantization interval
q_level=sig_nmax+Delta/2:sig_pmax-Delta/2; % define Q-levels
L_sig=length(sig_in); % find signal length
sigp=(sig_in-sig_nmax)/Delta+1/2; % convert into 1/2 to L+1/2 range
qindex=round(sigp); % round to 1,2, ... L levels
qindex=min(qindex,L); % eliminate L+1 as a rare possibility
q_out=q_level(qindex); % use index vector to generate output
SQNR=20*log10(norm(sig_in)/norm(sig_in-q_out)); % actual SQNR value
end
And:
% (sampandquant.m)
function [s_out,sq_out,sqh_out,Delta,SQNR]=sampandquant(sig_in,L,td,ts)
% Usage
% [s_out,sq_out,sqh_out,Delta,SQNR=sampandquant(sig_in,L,td,ts)
% L - number of uniform quantization levels
% sif_in - input signal vector
% td - origional signal sampling period of sig_in
% ts - new sampling period
% NOTE: td*fs must be a positive integer;
% Function outputs:
% s_out - sampled output
% sq_out - sample-and-quantized output
% sqh_out - sample, quantize, and hold output
% Delta - quantization interval
% SQNR - actual signal to quantization noise ratio
if (rem(ts/td,1)==0)
nfac=round(ts/td);
p_zoh=ones(1,nfac);
s_out=downsample(sig_in,nfac);
[sq_out,Delta,SQNR]=uniquan(s_out,L);
s_out=upsample(s_out,nfac);
sqh_out=kron(sq_out,p_zoh);
sq_out=upsample(sq_out,nfac);
else
warning('Error! ts/td is not an integer!')
s_out=[];sq_out=[];sqh_out=[];Delta=[];SQNR=[];
end
end

Risposte (1)

Sudheer Bhimireddy
Sudheer Bhimireddy il 9 Ago 2020
Try debugging by placing breakpoints inside the functions.
Here is what I see. Your q_level array is size 1x4 while the q_index has a size 1x51 or signal length is 51. You may want to define sufficient Q-levels in order to use q_level(qindex).
  2 Commenti
Zach Fisher
Zach Fisher il 9 Ago 2020
Becasue of your comment, I was able to debug and get the error corrected. Thank you so much.
Fares Saleh
Fares Saleh il 29 Nov 2020
Please can you show me what is the part you corrected so ican apply it because i have the same problem
Thank you

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by