Generate a 4-Hz, 1000-point sine wave with a sample interval of T=0.002, and use quantiziation to digitize it using a 4-,8-,12-, and 16-bit ADC...

25 views (last 30 days)
Hi, I'm having some issues debugging the for-loop involved with this problem and getting the output results I need. The problem is written below:
Generate a 4-Hz, 1000-point sine wave with a sample interval of T=0.002, and
use quantization to digitize it using a 4-,8-,12-, and 16-bit ADC. Then
subtract the original signal from the quantized signal to find the error
signal. The amplitude of the error signal should be equal to the quantization
level, q. Use the "max" and "min" functions in MATLAB to find this amplitude
and compare it with the theoretical value. Put this code in a for-loop and
repeat the evaluations for the four different bit levels requested.
Be sure to display the results to at least 4 decimal places to make an
accurate comparison.
I think for the most part I'm just not processing my for-loop correctly, and I'm not sure what I may need to modify in that for-loop to get it working correctly to provide me the outputs I'm looking for.
Should I try putting the disp(out) formatting outside of the for-loop too? Also, if I were to plot this sine wave - what command lines would I need to add to the m-file program to get the plot shown?
I'm assuming I would use the 'plot()' command somehow. But what if I wanted 4 different plots? One for each level of digitization I use in this problem (4, 8, 12, and 16) to represent the 4-bit, 8-bit, 12-bit, and 16-bit ADC digitization.
If anyone can provide me help with solving this problem, and send me solution feedback - I would greatly appreciate it. Thank you.
This is the code I have written so far:
T = 0.002; %sampling interval = 0.002
N = 1000; %Number of points = 1000
f = 4; %frequency = 4
b = 0; %bits = 0
t = (0:N-1)*T; %Vector used to generate a 1-cycle sine wave
Mx = 0; %I'm Initializing the maximum amplitude variable.
Mn = 0; %I'm Initializing the minimum amplitude variable.
for (b = 4: b<=16: b+4)
signal_in = sin(2*pi*frequency*t); %My input signal.
signal_out = quantization(signal_in, b); %Here, I am quantizing
%the output signal.
noisesignal = signal_out - signal_in; %This is my quantization error
%signal.
q_noise = var(noisesignal); %I'm calculating the variance
%of the quantization noise.
q = 1/(2^b - 1); %I'm calculating the quantization level.
theoretical = (q^2)/12; %I'm calculating the theoretical quantization
%error.
Mx = max(noisesignal); %I'm calculating the maximum amplitude
%of the quantization error signal.
Mn = min(noisesignal); %I'm calculating the minimum amplitude
%of the quantization error signal.
disp('Quantization Noise')
disp('Bits Empirical Theoretical')
out=sprintf('%2d %5e %5e', b, q_noise, theoretical);
%I'm Formatting my output
disp('Max Min')
out=sprintf('%2d %5e', Mx, Mn'); %I'm formatting my output for the max
%and min amplitudes of the quantization
%error signal.
disp(out)
end

Answers (2)

Jim Riggs
Jim Riggs on 1 Feb 2018
The first thing I notice is that signal_in is computed from
signal_in = sin(2*pi*frequency*t)
but variable "frequency" is not defined. It is defined as "f" in your code. The code would not run with this error, so I assume that it has been fixed and this is not the problem.
To plot the signal, you must create a figure window:
figure;
plot(t,signal_in,'b') % blue line plot
hold;
plot(t,signal_out,'r'); % red line plot
plot(t,noise,'k'); % black line plot
grid;
legend ('Input', 'Quantized', 'Noise');
The other thing that looks suspicious to me is the "quantization" function. This does not look like a built-in function, so I assume it is a user function. The built in function "quant" requires the second input to be the quantization magnitude, so it would look like:
signal_out = quant(signal_in,q)
Does this help?

Michael
Michael on 1 Feb 2018
Sorry, you are correct about the "frequency" part being undefined. I've corrected that typo in my code but when I run the program - I'm still not getting any amplitude outputs. It's acting like it is processing the for-loop okay but it is not producing any actual display output. :(
  3 Comments
Michael
Michael on 1 Feb 2018
My textbook contains an example of how the quantize signal is used. And that's the format I'm trying to stick with using:
%Example 1.4: Evaluate the quantization equation, Eq. 1.8 using
%simulated data.
%
f=4; %Desired frequency.
N=1000; %Number of points.
Ts=0.002; %Ts
bits=6; %Quantization level
t=(0:N-1)*Ts; %Vector used to generate a 1-cycle sine wave
signal_in = sin(2*pi*f*t); %Generate signal
signal_out = quantization(signal_in, bits); %Quantize signal
noise_signal = signal_out - signal_in;
%Determines quantization error
q_noise = var(noise_signal); %Variance of quantization noise
q = 1/(2^bits - 1); %Calculate quantization level (Eq. 1.6)
theoretical = (q^2)/12; %Theoretical quantization error (Eq. 1.8)
disp('Quantization Noise')
disp('Bits Empirical Theoretical')
out=sprintf('%2d %5e %5e, bits, q_noise, theoretical);
%Format the output
disp(out)
That's the example my textbook gives me exactly line-by-line. So I'm pretty sure quantization(signal_in, bits) does work unless Matlab has changed the command to something else lately due to version updates - which is possible I guess.
And no, I'm not getting any plots when I run the file. It's supposed to give me plots in addition to amplitude outputs. Like say if I enter: "max" into the Command window, then it might give me something like: "max = 3.2456e-8" or something like that. That's what I mean by amplitude outputs. I need the max and min amplitudes of the quantization error signal so I can compare it with my theoretical value.
Jim Riggs
Jim Riggs on 2 Feb 2018
What I am saying is that you should verify that the function is giving the expected/desired output by plotting it along with the input signal. Does it look the way you expect it to look? Then you will know that it is working and being used correctly.
I recommend that you a) forget about the for loop until you can get a single case to work correctly. b) make the plots using the code I suggested above to verify the operation of your code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!