Real-Time DAC with DSP tool box
I am having trouble implementing a real time DAC in my code. I am working with an NI USB-6356 and I have succesfully implemented in my Matlab Code an ADC than reads the real time signal and process it. I am trying to implement a DAC with the DSP tool box (as I did with the ADC) but for some reason it doesn’t work. There aren’t any apparent errors so it is being difficult to solve the problem. Can someone tell me what’s happening?
dev = ‘Dev1’;
channels = {‘ai0’, ‘ai1’};
ao = ‘ao0’; % DAC output channel
fs = 1000; % Sampling Frequency
fc = 10; % Carrier Frequency
fsub = 10; % Subcarrier Frequency
D = 1; % Decimation factor
bits = 16; % DAC Resolution
Kc = 0.01; % Proportional control gain
% Create TimeScope for visualization
scope = timescope(‘NumInputPorts’, 3, ‘TimeSpan’, 2, ‘SampleRate’, fs, …
‘ShowLegend’, true, ‘YLimits’, [-5 5], …
‘Title’, ‘Real-time Signal and Processed Signal’, …
‘ChannelNames’, {‘DAC Output’, ‘Input Signal’, ‘Modulated Signal’});
%% Data Acquisition Setup
daqSession = daq.createSession(‘ni’);
for i = 1:length(channels)
addAnalogInputChannel(daqSession, dev, channels{i}, ‘Voltage’);
end
daqSession.Rate = fs;
daqSession.IsContinuous = true;
addAnalogOutputChannel(daqSession, dev, ao, ‘Voltage’);
% Initialize NCO phase accumulator
nco_phase = 0;
lh = addlistener(daqSession, ‘DataAvailable’, …
@(src, event) processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D));
% Create an initial buffer to queue for DAC output
initialDACData = zeros(fs, 1); % Buffer of zeros to start with (length depends on initial session requirements)
queueOutputData(daqSession, initialDACData);
startBackground(daqSession);
disp(‘Press any key to stop’);
pause;
stop(daqSession);
delete(lh);
release(scope);
disp(‘Stopping data acquisition…’);
%% Function to process the signal and update DAC output
function OUT_signal = processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D)
% Input signal from ADC
signal = event.Data;
input_signal = signal(:, 2); % Input signal (from ai1)
dac_signal = signal(:, 1); % Read back the DAC signal (from ai0)
%===MANUAL NUMERICALLY CONTROLLED OSCILLATOR===
t = event.TimeStamps; % Time vector
% Set the control to adjust fc every second
onesec = 0;
tic;
persistent elapsed_time
if isempty(elapsed_time)
elapsed_time = 0;
end
% Update elapsed time
elapsed_time = elapsed_time + t(end) – t(1); % Time since last callback
% When elapsed time exceeds 1 second, update carrier frequency
if elapsed_time >= 1
% Save the current carrier frequency
% Calculate new fc using your control law: fc = fc + Kc * atan(Ik1/Qk1)
fc = fc + Kc * atan(Ik1/Qk1);
% Reset elapsed time
elapsed_time = 0;
onesec = toc;
end
% Generate DAC output
OUT_Signal = 0.5*sin(2*pi*(fc/fs)*t); % Using filtered AM as DAC signal
% Write to DAC (update Analog Output Channel)
src.queueOutputData(OUT_signal);
% Visualization
scope(input_signal, dac_signal, OUT_Signal);
endI am having trouble implementing a real time DAC in my code. I am working with an NI USB-6356 and I have succesfully implemented in my Matlab Code an ADC than reads the real time signal and process it. I am trying to implement a DAC with the DSP tool box (as I did with the ADC) but for some reason it doesn’t work. There aren’t any apparent errors so it is being difficult to solve the problem. Can someone tell me what’s happening?
dev = ‘Dev1’;
channels = {‘ai0’, ‘ai1’};
ao = ‘ao0’; % DAC output channel
fs = 1000; % Sampling Frequency
fc = 10; % Carrier Frequency
fsub = 10; % Subcarrier Frequency
D = 1; % Decimation factor
bits = 16; % DAC Resolution
Kc = 0.01; % Proportional control gain
% Create TimeScope for visualization
scope = timescope(‘NumInputPorts’, 3, ‘TimeSpan’, 2, ‘SampleRate’, fs, …
‘ShowLegend’, true, ‘YLimits’, [-5 5], …
‘Title’, ‘Real-time Signal and Processed Signal’, …
‘ChannelNames’, {‘DAC Output’, ‘Input Signal’, ‘Modulated Signal’});
%% Data Acquisition Setup
daqSession = daq.createSession(‘ni’);
for i = 1:length(channels)
addAnalogInputChannel(daqSession, dev, channels{i}, ‘Voltage’);
end
daqSession.Rate = fs;
daqSession.IsContinuous = true;
addAnalogOutputChannel(daqSession, dev, ao, ‘Voltage’);
% Initialize NCO phase accumulator
nco_phase = 0;
lh = addlistener(daqSession, ‘DataAvailable’, …
@(src, event) processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D));
% Create an initial buffer to queue for DAC output
initialDACData = zeros(fs, 1); % Buffer of zeros to start with (length depends on initial session requirements)
queueOutputData(daqSession, initialDACData);
startBackground(daqSession);
disp(‘Press any key to stop’);
pause;
stop(daqSession);
delete(lh);
release(scope);
disp(‘Stopping data acquisition…’);
%% Function to process the signal and update DAC output
function OUT_signal = processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D)
% Input signal from ADC
signal = event.Data;
input_signal = signal(:, 2); % Input signal (from ai1)
dac_signal = signal(:, 1); % Read back the DAC signal (from ai0)
%===MANUAL NUMERICALLY CONTROLLED OSCILLATOR===
t = event.TimeStamps; % Time vector
% Set the control to adjust fc every second
onesec = 0;
tic;
persistent elapsed_time
if isempty(elapsed_time)
elapsed_time = 0;
end
% Update elapsed time
elapsed_time = elapsed_time + t(end) – t(1); % Time since last callback
% When elapsed time exceeds 1 second, update carrier frequency
if elapsed_time >= 1
% Save the current carrier frequency
% Calculate new fc using your control law: fc = fc + Kc * atan(Ik1/Qk1)
fc = fc + Kc * atan(Ik1/Qk1);
% Reset elapsed time
elapsed_time = 0;
onesec = toc;
end
% Generate DAC output
OUT_Signal = 0.5*sin(2*pi*(fc/fs)*t); % Using filtered AM as DAC signal
% Write to DAC (update Analog Output Channel)
src.queueOutputData(OUT_signal);
% Visualization
scope(input_signal, dac_signal, OUT_Signal);
end I am having trouble implementing a real time DAC in my code. I am working with an NI USB-6356 and I have succesfully implemented in my Matlab Code an ADC than reads the real time signal and process it. I am trying to implement a DAC with the DSP tool box (as I did with the ADC) but for some reason it doesn’t work. There aren’t any apparent errors so it is being difficult to solve the problem. Can someone tell me what’s happening?
dev = ‘Dev1’;
channels = {‘ai0’, ‘ai1’};
ao = ‘ao0’; % DAC output channel
fs = 1000; % Sampling Frequency
fc = 10; % Carrier Frequency
fsub = 10; % Subcarrier Frequency
D = 1; % Decimation factor
bits = 16; % DAC Resolution
Kc = 0.01; % Proportional control gain
% Create TimeScope for visualization
scope = timescope(‘NumInputPorts’, 3, ‘TimeSpan’, 2, ‘SampleRate’, fs, …
‘ShowLegend’, true, ‘YLimits’, [-5 5], …
‘Title’, ‘Real-time Signal and Processed Signal’, …
‘ChannelNames’, {‘DAC Output’, ‘Input Signal’, ‘Modulated Signal’});
%% Data Acquisition Setup
daqSession = daq.createSession(‘ni’);
for i = 1:length(channels)
addAnalogInputChannel(daqSession, dev, channels{i}, ‘Voltage’);
end
daqSession.Rate = fs;
daqSession.IsContinuous = true;
addAnalogOutputChannel(daqSession, dev, ao, ‘Voltage’);
% Initialize NCO phase accumulator
nco_phase = 0;
lh = addlistener(daqSession, ‘DataAvailable’, …
@(src, event) processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D));
% Create an initial buffer to queue for DAC output
initialDACData = zeros(fs, 1); % Buffer of zeros to start with (length depends on initial session requirements)
queueOutputData(daqSession, initialDACData);
startBackground(daqSession);
disp(‘Press any key to stop’);
pause;
stop(daqSession);
delete(lh);
release(scope);
disp(‘Stopping data acquisition…’);
%% Function to process the signal and update DAC output
function OUT_signal = processSignal(src,event, fc, fs, fsub, scope, nco_phase, Kc, D)
% Input signal from ADC
signal = event.Data;
input_signal = signal(:, 2); % Input signal (from ai1)
dac_signal = signal(:, 1); % Read back the DAC signal (from ai0)
%===MANUAL NUMERICALLY CONTROLLED OSCILLATOR===
t = event.TimeStamps; % Time vector
% Set the control to adjust fc every second
onesec = 0;
tic;
persistent elapsed_time
if isempty(elapsed_time)
elapsed_time = 0;
end
% Update elapsed time
elapsed_time = elapsed_time + t(end) – t(1); % Time since last callback
% When elapsed time exceeds 1 second, update carrier frequency
if elapsed_time >= 1
% Save the current carrier frequency
% Calculate new fc using your control law: fc = fc + Kc * atan(Ik1/Qk1)
fc = fc + Kc * atan(Ik1/Qk1);
% Reset elapsed time
elapsed_time = 0;
onesec = toc;
end
% Generate DAC output
OUT_Signal = 0.5*sin(2*pi*(fc/fs)*t); % Using filtered AM as DAC signal
% Write to DAC (update Analog Output Channel)
src.queueOutputData(OUT_signal);
% Visualization
scope(input_signal, dac_signal, OUT_Signal);
end dsp, signal processing, daq, dac MATLAB Answers — New Questions