how to solve Index exceeds the number of array elements (1)?

1 visualización (últimos 30 días)
Loong Pavel
Loong Pavel el 27 de Oct. de 2021
Respondida: Walter Roberson el 28 de Oct. de 2021
clear, clc
% Set up time and frequency vectors
Ts = 1e-6; t = 0:Ts:1-Ts; N = length(t); % Time vectors
fs = 1/Ts; F = fs/N; f = (-fs/2):F:(fs/2)-F; % Frequency vectors
% Signal to be processed
A1 = 1.1;
A2 = 0.5;
f1 = 500;
f2 = 1500;
x = A1*cos((2*pi*f1*t)-(pi/6))+A2*cos((2*pi*f2*t)+(pi/4)); % Original signal
% Plot original signal
figure(1)
subplot(2,1,1), plot(t, x)
xlabel('Time, sec', 'Fontsize', 14)
ylabel('x(t)', 'Fontsize', 14)
hold on
% Simulate sampling this signal
fsamp = 4000; % sampling rate, choose Nyquist rate and 2 x Nyquist rate
% Simulate sampling this signal
Tsamp = 1/fsamp; % Sampling period
y = x(1:round(Tsamp/Ts):end); % This is the sampled signal!!
% Plot this on top of original signal
tsamp = t(1:round(Tsamp/Ts):end); % Set up new time array to plot samples against
stem(tsamp, y), axis([0 0.02 min(x)*1.5 max(x)*1.5])
grid on
hold off
% Quantiser information
n = 4; % Number of bits (n-bit quantiser)
L = 16; % Total number of quantisation levels
V_range = 3; % Total voltage range of quantiser (max p-p amplitude of incoming signal)
D = 0.1875; % Voltage spacing between quantisation levels
Vo = -1.40625; % Initial quantisation level
codebook = 0; % Vo + all remaining quantisation levels in an array e.g. codebook = [Vo V1 V2 V3 etc...];
% Quantise signal
encoded = myQuantiser(y, codebook); % Call function to perform quantisation on signal
% myQuantiser() requires the signal to be quantised (y) and the codebook
% which is all the quantisation levels in an array
% e.g. codebook = [Vo V1 V2 V3 etc...];
subplot(2,1,2), stairs(tsamp, encoded), hold on
plot(t, x), hold off
xlabel('Time, sec', 'Fontsize', 14)
ylabel('x(t)', 'Fontsize', 14)
axis([0 0.02 min(x)*1.5 max(x)*1.5])
grid on
I got a problem about Index exceeds the number of array elements (1) in line 51. How to solve it? Thanks.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Oct. de 2021
codebook = 0; % Vo + all remaining quantisation levels in an array e.g. codebook = [Vo V1 V2 V3 etc...];
Look at the comments. codebook is expected to be a vector of all of the quantization levels, but instead you have made it the scalar 0.
You should be using V0 and D and V_range to calculate the quantization levels.

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by