[Ch4 ADC/DAC] How to simulate ADC/DAC process in Matlab? How to actually reconstruct a signal nearly sampled in Nyquist Rate?

Reconstruction is essentially a kind of interpolation or so-called digital to analog conversion (DAC). Detail descriptions are introduced in chapter 4.8.3 of the DSP Bible [1].
Although we all understand the basic sampling theory, the problem is how to bridge the theory and practice. Because all the signals we can simulate in Matlab are essential 'Digital'. So, the so-cal ADC/DAC is just an approaching simulation. Let me briefly justify the difference.


Ideal Reconstruction in DAC: There is typically a zero order hold circuit and reconstruction filter. Please note that the frequency response of zero-order hold is not evenly flat so a reconstruction filter comes into an aid.
Matlab Simulation in DAC: There is no need to actually simulate a zero-order hold. All you need are an upsampling and an ideal low pass filter.
This sample code sample a simulated 2 Hz analog signal,x_a, (with sampling rate 1500 Hz) to a discrete-time signal,x_d, with 5 Hz sampling rate (nearly Nyquist rate) and then reconstruct this back,x_r. 
    %% This code simulate the AD/DA processing discussed in Chapter 4.8.3 [1]
    close all; clear all;

    %% parameters.
    % analog
    analog_fps = 1500;
    analog_window_time = 3; %sec
    t = 0:1/analog_fps: analog_window_time-1/analog_fps;
    
    % digital
    digital_fps = 5;
    n = downsample(t,analog_fps/digital_fps);
    
    % ADC: Quantizer
    X_m = 1; % Range
    B = 10;% Bit number.
    
    %% Signal generation
    freq_hz = 1; % Hz.
    x_a_1 = 0.5*cos(2*pi*freq_hz*t+0.1);

    % add a small high frequency component as asked.       
    signal_freq = 2; %Hz
    x_a_2 = 0.5*cos(2*pi*signal_freq*t+pi/2);
    x_a = x_a_1 + x_a_2;
    
    %% ADC
    % Sampling
    x_s = downsample(x_a,analog_fps/digital_fps);
    
    % Quantizing (abs of input value should not over 1)
    % x_d = Quantizing(x_s,B,X_m);  % A For complete ADC, a quantizing should
    % be added here.
    x_d = x_s; % For basic case, we skip the  quantizing here.
    
    %% DAC
    % up sample / DAC
    x_up = upsample(x_d,analog_fps/digital_fps);

    % LPF (Reconstruction Filters)
    h = intfilt(analog_fps/digital_fps,4,0.9); 
    %% Important
    % please not the parameter 0.9, ideally should be 1 for Nyquist rate.
    % 0.9 here is ratio of Nyquist.
    % Given known limit band signal, shourter ratio can enhance SNR by oversampling.
    %  (i,e, here I filterout the freq larger than 2.5(Nyquist rate) * 0.9 = 2.25Hz)

    x_r = filter(h,1,x_up);
    x_r(1:floor(mean(grpdelay(h)))) = [];
    x_r = [x_r zeros(1,floor(mean(grpdelay(h))))];
    
    
    %% Display

    figure;
    plot(t,x_a);
    hold on;
    plot(n,x_d);
    plot(t,x_r);
    title('analog signal (1500Hz) v.s. digital signal (5Hz) v.s. Reconstructed signal (1500Hz)');
    legend('x_a','x_d','x_r');


Basic Result: 


You can adjust the digital_fps with sampling rate higher than Nyquist rate (30 for example) and 0.9 in intfilt to 0.5 (For easier filter design and shorter group delay). 






Comments

Popular posts from this blog

[Ch7. Filter Design] Why is it a bad idea to filter by zeroing out FFT bins? (為何不能DFT轉過去,直接設成想要的形狀,再IDFT轉回來?)

[Chapter 5.1] Meaning of General Linear Phase or Group Delay of a filter?