MATLAB: What is wrong with this code, please help!

quantize sine wave

>> t = [0:.1:2*pi]; % Times at which to sample the sine function
sig = sin(t); % Original signal, a sine wave
partition = [-1:.2:1]; % Length 11, to represent 12 intervals
codebook = [-1.2:.2:1]; % Length 12, one entry for each interval
[index,quants] = quantiz(sig,partition,codebook); % Quantize.
plot(t,sig,'x',t,quants,'.')
legend('Original signal','Quantized signal');
axis([-.2 7 -1.2 1.2])
>>
>> M = 4;
>> y = dpskmod(quants,M,pi / 8 ) ;
but i still keep getting the error
??? Error using ==> dpskmod at 41
Elements of input X must be integers in the range [0, M-1].

Best Answer

Your codebook needs to be 0:3 (the integers) if you want to use M=4. However, you are quantizing into 12 bins that you presumably want to distinguish, and that implies 0:11 for the codebook and M=12.