- Incorrect Sampling rate: verify the sampling rate. It must not be set too high or too low for the device capabilities. So, Verify the device specifications and adjust “daqSession.Rate” accordingly.
- Try updating the driver Toolbox.
- Try logging the output signals to get better understanding of the picture.
Real-Time DAC with DSP tool box
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Ayush
2024-10-3
I understand that your code may be encountering issues despite not displaying any errors.
When working with hardware interfacing, some potential sources of errors you can consider at:
Although your code’s logic seems fine, these errors could be the potential reasons your code might be failing.
Hope this helps. If the issue is persistant then if possible, kindly provide a little more description of the actual behavior of the code versus the expected behavior so that we can compare the results and find the root cause.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!