How to change a parameter while the signal is running?

11 views (last 30 days)
Hello everyone,
I want to generate a signal that can be changed while running. I want to be able to change the magnitude while the signal is running. I tried generating a signal in background and i wrote different values for the magnitude in the command box but it didn't change anything.
I have the following code:-
%-------------------------------------
clearvars
clc
d = daqlist("ni");
deviceInfo = d{1, "DeviceInfo"};
dq = daq("ni");
fidData = fopen("logData.bin","w");
fidTime = fopen("logTime.bin","w");
DisplayTime=1;
s = daq.createSession('ni');
s.Rate=5000;
addAnalogInputChannel(s,'Dev2','ai11','Voltage');
lh = addlistener(s,'DataAvailable',@plotData);
lh1 = addlistener(s,'DataAvailable',@(src, event)logData(src, event, fidData,fidTime));
s.IsContinuous=true;
s.NotifyWhenDataAvailableExceeds=floor(DisplayTime*s.Rate);
%Output
addAnalogOutputChannel(s,'Dev2','ao0','Voltage');
Fs=s.Rate
t1=100;
t = 0 :1/Fs : t1-1/Fs;
A=0.5;
data0 = A*sin(2*pi*t*159.2)';
b=repmat(data0,5,1);
queueOutputData(s,b);
datagiver = addlistener(s, 'DataRequired',@(src,event)...
src.queueOutputData(b));
figure(1)
clf
startBackground(s);
%%
s.stop
fclose(fidData);
fclose(fidTime);
%-------------------------------------
Please help if you have an idea. I need to do this because im making a controller for the magnitude value.
Regards
Ali

Answers (1)

Satwik
Satwik on 21 Mar 2025
Hi,
In order to dynamically change the magnitude of the output signal while the DAQ session is running, we need to modify the queued output data in real-time. This can be accomplished by updating the data in the 'DataRequired' event. Below is a modified version of the code to achieve this, with comments explaining the changes made:
% Setup code for the session same as before
% Add listeners for data available and logging
lh = addlistener(s, 'DataAvailable', @plotData);
lh1 = addlistener(s, 'DataAvailable', @(src, event) logData(src, event, fidData, fidTime));
% Set session properties
s.IsContinuous = true;
s.NotifyWhenDataAvailableExceeds = floor(DisplayTime * s.Rate);
% Output setup
addAnalogOutputChannel(s, 'Dev2', 'ao0', 'Voltage');
Fs = s.Rate;
t1 = 100;
t = 0:1/Fs:t1-1/Fs;
A = 0.5; % Initial magnitude
data0 = A * sin(2 * pi * t * 159.2)';
b = repmat(data0, 5, 1);
queueOutputData(s, b);
% Listener for updating output data with new magnitude
datagiver = addlistener(s, 'DataRequired', @(src, event) updateOutputData(src));
% Start the session in the background
figure(1)
clf
startBackground(s);
% Function to update the output data
function updateOutputData(src)
newMagnitude = getNewMagnitude(); % This function should provide the updated magnitude
t = 0:1/src.Rate:1-1/src.Rate; % Generate time vector for one second
data0 = newMagnitude * sin(2 * pi * t * 159.2)'; % Update data with new magnitude
b = repmat(data0, 5, 1);
src.queueOutputData(b);
end
% Function to get a new magnitude value (stub for illustration)
function magnitude = getNewMagnitude()
% Replace this with your own logic to get the new magnitude
% For example, reading from a GUI control or an external input
magnitude = 0.5; % Example fixed value, replace with dynamic logic
end
% Stop the session and close files
s.stop
fclose(fidData);
fclose(fidTime);
I hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!