Working with Real-Time signals in matlab
I am trying to work with a real time signal witht the DSP Tool box but I am finding difficult to acomplish. I have seen the demos and read the documents related with the DSP Tool box but it doesn’t really seem Matlab can work/process real time signals. My objective was to do a DAC to create a signal, an ADC to read this same signal, process it, and then do another DAC to see the processed signal. I have been able to work out an ADC and a DAC but none of the options available (at least for the 2022a version) are actually real time but trying to simulate it.
I will share the code in case anyone has curiostity, but not only it fails when approaching the frequency too nyquist’s but also seems to have a hard time when the processing is just multiplying by two, can’t imagine what would happen if I modulated the signal…
%============================================================%
%============== Continuous Real Time DAC & ADC===============%
%============================================================%
%========= Processes Data and Displays it Real Time =========%
%========= DisplaySignalV5: More advanced processes ========%
%============================================================%
% REQUIRED: Signal Processing Toolbox & DSP System Toolbox (NI Extension)
clear;
close all;
clc;
% === DAC and ADC === %
% Create DataAcquisition object for NI device
dq = daq("ni");
% Add Analog Output Channels to generate the signal on "ao0" and "ao1"
addoutput(dq, "Dev1", "ao0", "Voltage");
addoutput(dq, "Dev1", "ao1", "Voltage");
ch_out = dq.Channels(1:2);
ch_out(1).Name = "ao0_out";
ch_out(2).Name = "ao1_out";
% Add an Analog Input Channel to acquire the signal on "ai1"
addinput(dq, "Dev1", "ai1", "Voltage");
ch_in = dq.Channels(3);
ch_in.Name = "ai1_in";
% Set the sample rate
rate = 10000; % Hz
dq.Rate = rate;
% Specify the output signal – A square wave with a DC offset
f = 50; % Frequency of 10 Hz
t = (0:rate-1)/rate; % Time vector for 1 second
output = 1.25 + 0.25*square(2*pi*f*t); % Generate square wave with DC offset
% Create timescope to visualize the signals in real time
scope = timescope(‘SampleRate’, rate, ‘TimeSpanSource’, ‘Property’, ‘TimeSpan’, 0.1, …
‘YLimits’, [-2 2], ‘Title’, ‘Output vs Input Signals’, …
‘ShowLegend’, true, ‘ChannelNames’, {‘Inintal Signal DAC_1’, ‘Processed Signal DAC_2’});
% Set the callback function to execute when data is available
batchSize = rate; % Read data for 1 second at a time for more frequent updates
dq.ScansAvailableFcnCount = batchSize;
dq.ScansAvailableFcn = @(src, evt) plotAndWriteSignals(src, output, scope, rate);
% Preload the output data for "ao0"
preloadData = [output’, zeros(length(output), 1)]; % Preload output for ao0, set ao1 to zero initially
preload(dq, preloadData);
% Start the output and input simultaneously in continuous mode
start(dq, "repeatoutput");
% Stop the generation and acquisition after key press
disp(‘Press any key to stop the continuous generation and acquisition.’);
pause();
stop(dq);
disp("Generation and acquisition stopped.");
% Callback function to read data, plot input and output using timescope, and write to ao1 in real-time
function plotAndWriteSignals(src, output, scope, rate)
% Read available data in chunks as defined by ScansAvailableFcnCount
[data, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
processedSignal = 2.*data;
% Ensure that both ‘data’ and ‘outputToPlot’ are column vectors and have the same length
len = length(processedSignal);
outputToPlot = output(1:len)’;
if isrow(processedSignal)
processedSignal = processedSignal’;
end
% Update the timescope with the output and input signals
% The input data and output data must have consistent row sizes
scope([outputToPlot, processedSignal]);
% Write the acquired input data to the output channel "ao1"
% Create a matrix where each row contains values for both output channels
outputData = [outputToPlot, processedSignal]; % First column for ao0, second column for ao1
write(src, outputData);
endI am trying to work with a real time signal witht the DSP Tool box but I am finding difficult to acomplish. I have seen the demos and read the documents related with the DSP Tool box but it doesn’t really seem Matlab can work/process real time signals. My objective was to do a DAC to create a signal, an ADC to read this same signal, process it, and then do another DAC to see the processed signal. I have been able to work out an ADC and a DAC but none of the options available (at least for the 2022a version) are actually real time but trying to simulate it.
I will share the code in case anyone has curiostity, but not only it fails when approaching the frequency too nyquist’s but also seems to have a hard time when the processing is just multiplying by two, can’t imagine what would happen if I modulated the signal…
%============================================================%
%============== Continuous Real Time DAC & ADC===============%
%============================================================%
%========= Processes Data and Displays it Real Time =========%
%========= DisplaySignalV5: More advanced processes ========%
%============================================================%
% REQUIRED: Signal Processing Toolbox & DSP System Toolbox (NI Extension)
clear;
close all;
clc;
% === DAC and ADC === %
% Create DataAcquisition object for NI device
dq = daq("ni");
% Add Analog Output Channels to generate the signal on "ao0" and "ao1"
addoutput(dq, "Dev1", "ao0", "Voltage");
addoutput(dq, "Dev1", "ao1", "Voltage");
ch_out = dq.Channels(1:2);
ch_out(1).Name = "ao0_out";
ch_out(2).Name = "ao1_out";
% Add an Analog Input Channel to acquire the signal on "ai1"
addinput(dq, "Dev1", "ai1", "Voltage");
ch_in = dq.Channels(3);
ch_in.Name = "ai1_in";
% Set the sample rate
rate = 10000; % Hz
dq.Rate = rate;
% Specify the output signal – A square wave with a DC offset
f = 50; % Frequency of 10 Hz
t = (0:rate-1)/rate; % Time vector for 1 second
output = 1.25 + 0.25*square(2*pi*f*t); % Generate square wave with DC offset
% Create timescope to visualize the signals in real time
scope = timescope(‘SampleRate’, rate, ‘TimeSpanSource’, ‘Property’, ‘TimeSpan’, 0.1, …
‘YLimits’, [-2 2], ‘Title’, ‘Output vs Input Signals’, …
‘ShowLegend’, true, ‘ChannelNames’, {‘Inintal Signal DAC_1’, ‘Processed Signal DAC_2’});
% Set the callback function to execute when data is available
batchSize = rate; % Read data for 1 second at a time for more frequent updates
dq.ScansAvailableFcnCount = batchSize;
dq.ScansAvailableFcn = @(src, evt) plotAndWriteSignals(src, output, scope, rate);
% Preload the output data for "ao0"
preloadData = [output’, zeros(length(output), 1)]; % Preload output for ao0, set ao1 to zero initially
preload(dq, preloadData);
% Start the output and input simultaneously in continuous mode
start(dq, "repeatoutput");
% Stop the generation and acquisition after key press
disp(‘Press any key to stop the continuous generation and acquisition.’);
pause();
stop(dq);
disp("Generation and acquisition stopped.");
% Callback function to read data, plot input and output using timescope, and write to ao1 in real-time
function plotAndWriteSignals(src, output, scope, rate)
% Read available data in chunks as defined by ScansAvailableFcnCount
[data, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
processedSignal = 2.*data;
% Ensure that both ‘data’ and ‘outputToPlot’ are column vectors and have the same length
len = length(processedSignal);
outputToPlot = output(1:len)’;
if isrow(processedSignal)
processedSignal = processedSignal’;
end
% Update the timescope with the output and input signals
% The input data and output data must have consistent row sizes
scope([outputToPlot, processedSignal]);
% Write the acquired input data to the output channel "ao1"
% Create a matrix where each row contains values for both output channels
outputData = [outputToPlot, processedSignal]; % First column for ao0, second column for ao1
write(src, outputData);
end I am trying to work with a real time signal witht the DSP Tool box but I am finding difficult to acomplish. I have seen the demos and read the documents related with the DSP Tool box but it doesn’t really seem Matlab can work/process real time signals. My objective was to do a DAC to create a signal, an ADC to read this same signal, process it, and then do another DAC to see the processed signal. I have been able to work out an ADC and a DAC but none of the options available (at least for the 2022a version) are actually real time but trying to simulate it.
I will share the code in case anyone has curiostity, but not only it fails when approaching the frequency too nyquist’s but also seems to have a hard time when the processing is just multiplying by two, can’t imagine what would happen if I modulated the signal…
%============================================================%
%============== Continuous Real Time DAC & ADC===============%
%============================================================%
%========= Processes Data and Displays it Real Time =========%
%========= DisplaySignalV5: More advanced processes ========%
%============================================================%
% REQUIRED: Signal Processing Toolbox & DSP System Toolbox (NI Extension)
clear;
close all;
clc;
% === DAC and ADC === %
% Create DataAcquisition object for NI device
dq = daq("ni");
% Add Analog Output Channels to generate the signal on "ao0" and "ao1"
addoutput(dq, "Dev1", "ao0", "Voltage");
addoutput(dq, "Dev1", "ao1", "Voltage");
ch_out = dq.Channels(1:2);
ch_out(1).Name = "ao0_out";
ch_out(2).Name = "ao1_out";
% Add an Analog Input Channel to acquire the signal on "ai1"
addinput(dq, "Dev1", "ai1", "Voltage");
ch_in = dq.Channels(3);
ch_in.Name = "ai1_in";
% Set the sample rate
rate = 10000; % Hz
dq.Rate = rate;
% Specify the output signal – A square wave with a DC offset
f = 50; % Frequency of 10 Hz
t = (0:rate-1)/rate; % Time vector for 1 second
output = 1.25 + 0.25*square(2*pi*f*t); % Generate square wave with DC offset
% Create timescope to visualize the signals in real time
scope = timescope(‘SampleRate’, rate, ‘TimeSpanSource’, ‘Property’, ‘TimeSpan’, 0.1, …
‘YLimits’, [-2 2], ‘Title’, ‘Output vs Input Signals’, …
‘ShowLegend’, true, ‘ChannelNames’, {‘Inintal Signal DAC_1’, ‘Processed Signal DAC_2’});
% Set the callback function to execute when data is available
batchSize = rate; % Read data for 1 second at a time for more frequent updates
dq.ScansAvailableFcnCount = batchSize;
dq.ScansAvailableFcn = @(src, evt) plotAndWriteSignals(src, output, scope, rate);
% Preload the output data for "ao0"
preloadData = [output’, zeros(length(output), 1)]; % Preload output for ao0, set ao1 to zero initially
preload(dq, preloadData);
% Start the output and input simultaneously in continuous mode
start(dq, "repeatoutput");
% Stop the generation and acquisition after key press
disp(‘Press any key to stop the continuous generation and acquisition.’);
pause();
stop(dq);
disp("Generation and acquisition stopped.");
% Callback function to read data, plot input and output using timescope, and write to ao1 in real-time
function plotAndWriteSignals(src, output, scope, rate)
% Read available data in chunks as defined by ScansAvailableFcnCount
[data, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
processedSignal = 2.*data;
% Ensure that both ‘data’ and ‘outputToPlot’ are column vectors and have the same length
len = length(processedSignal);
outputToPlot = output(1:len)’;
if isrow(processedSignal)
processedSignal = processedSignal’;
end
% Update the timescope with the output and input signals
% The input data and output data must have consistent row sizes
scope([outputToPlot, processedSignal]);
% Write the acquired input data to the output channel "ao1"
% Create a matrix where each row contains values for both output channels
outputData = [outputToPlot, processedSignal]; % First column for ao0, second column for ao1
write(src, outputData);
end dsp, signal processing, digital signal processing, ni-daq, analog input, analog output MATLAB Answers — New Questions