I have a continuous time signal $x(t)=\sin(2πft)$ where $0 \leq t \leq 3$.

I want to sample the continuous time signal and then quantize that sampled signal and then plot both sampled and quantized signals in MATLAB.

I have written code for sampling section/part but I don't know how to go with quantization part/section.

My MATLAB code is follow:

sampleRate = 500;
samplePeriod = 1/sampleRate;
signalFreq = 20;
nT = 0:samplePeriod:3;
signal = cos(2*pi*signalFreq*nT);

stem(nT, signal)
grid on
xlabel('Time (sec)')

  • 1
    $\begingroup$ Hmm...do you know @engr? Just saying you need to quantize the signal isn't enough, you need to provide more information such as number of bits you want to use to quantize since this will set the number of quantization levels $\endgroup$ – Engineer Feb 13 '20 at 14:19
  • 1
    $\begingroup$ Strictly speaking, all signals in Matlab are DT and quantized... computers don't have infnite precision. </nitpick> $\endgroup$ – MBaz Feb 13 '20 at 22:17
  • $\begingroup$ @Engineer I assumed abtj probably does know engr, but an investigation showed no direct vote abuse. So, maybe they are the same person, but at least they don't seem to systematically upvote themselves. $\endgroup$ – Marcus Müller Feb 16 '20 at 14:50

First you will need to determine the number of quantization levels. I am going to assume a power of two for digital convenience's sake.

 nbits = 8 % 256 qantization levels
 qLevels = 2^nbits 

The next step will be to scale your signal to have the same magnitude as your number of bits.

 signalMin = -1  
 signalMax = 1  
 scalingFactor = (signalMax-signalMin)/qLevels
 signal = signal / scalingFactor 

This gives you a signal ranging from -128 to 128.
You now have the choice of four functions to use:

floor()  %round down to the nearest integer  
round()  %round to the nearest integer  
ceil()   %round up to the nearest integer
fix()    %round towards zero

I will use round(), then scale the signal back to its original magnitude

signal = round(signal)
signal = signal * scalingFactor

You already seem to have the ability to graph down. I will leave graphing the function up to you.

Edit: To respond to abtj's comment about quant(): I wasn't familiar with quant, but it seems it would work just fine. The second argument needs to be the value of the least significant bit. This is the same as the scalingFactor as calculated in the code above.

The scalingFactor is simply a way to scale the original signal to the size of the quantization. i.e. scale a signal from -1 to 1 volts to ±8 for a 4 bit quantization. This is to make a function like round() useful, which will only round to integer values.
This is done by taking the range of the original signal (signalMax-signalMin) and dividing it by the number of quantization levels desired.

Please point out any bugs in my code; I don't have access to MatLab to test right now.

Edited: Swapped the * and / when scaling the signal.
Edited: Added Parentheses per A_A's comment.

  • 1
    $\begingroup$ (signalMax - signalMin)/qLevels $\endgroup$ – A_A Feb 14 '20 at 9:59
  • $\begingroup$ Thanks @A_A. Its been edited. $\endgroup$ – ambitiose_sed_ineptum Feb 14 '20 at 16:55
  • $\begingroup$ What do you mean by "signal" in right side of equation" signal = signal / scalingFactor" ?? do you mean continuous signal here or sampled version? $\endgroup$ – abtj Feb 14 '20 at 17:46
  • $\begingroup$ what command should be used for displaying graph of final quantized signal in last line of code?plot or stem or stairs? $\endgroup$ – abtj Feb 14 '20 at 17:50
  • $\begingroup$ @abjt the signal I used in the line you asked about is the same signal you have defined in your question: signal = cos(2*pi*signalFreq*nT); Which is a sampled signal. The plot type you are using depends on your specific use case; I'd probably go for stairs because it makes the discrete quantization clear. $\endgroup$ – ambitiose_sed_ineptum Feb 14 '20 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.