convert analog to digital using matlap and input i entered

H
hossammanar222 · Apr 27, 2022 · 2.3K views
Question
convert analog to digital using matlap
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Sep 2, 2026

To convert an analog signal to a digital signal (ADC) in MATLAB based on custom user input, you execute three fundamental stages: Sampling (discretizing continuous time at sampling frequency \(f_s \ge 2f_{max}\)), Quantization (rounding continuous amplitude values to \(2^n\) discrete voltage levels determined by ADC bit resolution \(n\)), and Encoding (mapping quantized levels to binary codewords using de2bi()).

Interactive MATLAB Code: Analog to Digital Conversion (ADC)

The following script accepts user inputs dynamically from the command window, performs uniform quantization, computes the binary bitstream, and plots the complete conversion process:

% ==============================================================
% Interactive Analog to Digital Converter (ADC) Simulation
% ==============================================================
clc;
clear;
close all;

% Step 1: Gather User Inputs
fprintf('=== Analog to Digital Converter (ADC) Parameters ===\n');
f_sig = input('Enter analog signal frequency in Hz (e.g., 5): ');
f_sample = input('Enter sampling frequency in Hz (e.g., 50): ');
n_bits = input('Enter ADC bit resolution (e.g., 3 or 8): ');
duration = input('Enter signal duration in seconds (e.g., 1): ');

% Validate Nyquist Criterion
if f_sample < 2 * f_sig
    warning('Sampling frequency is below Nyquist rate (2*f). Aliasing will occur.');
end

% Step 2: Generate Continuous-Time Analog Signal
t_analog = linspace(0, duration, 10000); % High-resolution time base
v_analog = sin(2 * pi * f_sig * t_analog); % Analog sine wave (-1V to +1V)

% Step 3: Sampling (Discrete-Time Conversion)
t_sample = 0:(1/f_sample):duration;
v_sample = sin(2 * pi * f_sig * t_sample);

% Step 4: Uniform Quantization
L = 2^n_bits;                      % Total quantization levels
v_min = -1.0;                      % Minimum voltage range
v_max = 1.0;                       % Maximum voltage range
delta = (v_max - v_min) / L;       % Quantization step size (resolution)

% Partition boundaries and codebook values
partition = (v_min + delta):delta:(v_max - delta);
codebook = (v_min + delta/2):delta:(v_max - delta/2);

% Map continuous samples to nearest discrete levels
[index, v_quantized] = quantiz(v_sample, partition, codebook);

% Step 5: Binary Encoding
binary_code = de2bi(index, n_bits, 'left-msb');

% Display Results in Command Window
fprintf('\n--- Conversion Summary ---\n');
fprintf('Quantization Levels: %d\n', L);
fprintf('Step Size (Delta): %.4f V\n', delta);
fprintf('First 5 Quantized Decimal Values & Binary Encodings:\n');
for k = 1:min(5, length(index))
    fprintf('Sample %d: Analog = %.3f V -> Quantized = %.3f V -> Binary = %s\n', ...
        k, v_sample(k), v_quantized(k), num2str(binary_code(k,:)));
end

% Step 6: Multi-Stage Visualization
figure('Name', 'Analog to Digital Conversion Stages', 'NumberTitle', 'off');

% 1. Original Analog Signal
subplot(4,1,1);
plot(t_analog, v_analog, 'b', 'LineWidth', 1.5);
grid on;
title('1. Continuous Analog Signal');
xlabel('Time (s)');
ylabel('Amplitude (V)');
ylim([v_min - 0.2, v_max + 0.2]);

% 2. Sampled Discrete Signal
subplot(4,1,2);
stem(t_sample, v_sample, 'r', 'LineWidth', 1.2, 'MarkerFaceColor', 'r');
grid on;
title(['2. Sampled Signal (f_s = ' num2str(f_sample) ' Hz)']);
xlabel('Time (s)');
ylabel('Amplitude (V)');
ylim([v_min - 0.2, v_max + 0.2]);

% 3. Quantized Staircase Signal
subplot(4,1,3);
stairs(t_sample, v_quantized, 'm', 'LineWidth', 1.5);
hold on;
plot(t_sample, v_sample, 'r.', 'MarkerSize', 8);
grid on;
title(['3. Quantized Output (' num2str(n_bits) '-Bit ADC, ' num2str(L) ' Levels)']);
xlabel('Time (s)');
ylabel('Quantized (V)');
ylim([v_min - 0.2, v_max + 0.2]);

% 4. Quantization Error Noise
subplot(4,1,4);
quant_error = v_sample - v_quantized;
plot(t_sample, quant_error, 'k--', 'LineWidth', 1.2);
grid on;
title('4. Quantization Error Noise [e(n) = x(n) - x_q(n)]');
xlabel('Time (s)');
ylabel('Error (V)');

Key Mathematical Formulas Behind the Process

ADC Parameter Formula Description
Nyquist Sampling Rate \(f_s \ge 2 f_{max}\) Minimum sampling rate required to avoid spectral aliasing.
Number of Levels (\(L\)) \(L = 2^n\) Total discrete quantization levels available for an \(n\)-bit ADC.
Quantization Step (\(\Delta\)) \(\Delta = \frac{V_{max} - V_{min}}{2^n}\) Voltage interval between two adjacent discrete digital steps.
Signal-to-Quantization-Noise Ratio \(SQNR \approx 6.02n + 1.76 \text{ dB}\) Theoretical signal fidelity improvement per additional bit of resolution.

Practical Summary of Conversion Steps

  • Sampling (Time Discretization): Converts the continuous waveform into discrete impulses at fixed intervals \(T_s = 1/f_s\).
  • Quantization (Amplitude Discretization): Maps continuous voltages to the closest discrete level within \(\pm \Delta/2\), introducing small quantization error noise.
  • Encoding (Binary Bitstream): Converts decimal quantization indices into \(n\)-bit binary code words ready for digital signal processors (DSPs), microcontrollers, or FPGA transmission.
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!