- The wrapper.getSpectrum() method blocks (i.e., does not return) until a new spectrum is available. When you call wrapper.getSpectrum() in one thread, it only blocks that thread. It does not impact any other thread.
How do I record from two spectrometers simultaneously
3 views (last 30 days)
Show older comments
I am running two OceanOptics Spectrometers with the instrument control toolbox, and need them to record at the same time
With the function below, a given spectrometer (object) will run for a set number of iterations, and plots the spectrum. The sampling time is preset outside of the function. A slight variation is when I save the spectrum, but I don't think needs including here.
The invoke('getSpectrum') begins it recording, and then creates an array with one value per detector pixel. The issue is that if I want to run two objects within the same loop, it records the first, then records the second etc.
I've tried looking at the parallel processing toolbox but the only information I found was on running two functions that return data, which is not the case for this particular function
function spectrometerLive(iterations, spectrometer)
spectrometerIndex = 0;
channelIndex = 0;
wavelengths = invoke(spectrometer, 'getWavelengths', spectrometerIndex, channelIndex);
figure();
for i = 1:iterations
spectralData = invoke(spectrometer, 'getSpectrum', 0);
plot(wavelengths,spectralData)
title('Optical Spectrum');
ylabel('Intensity (counts)');
xlabel('\lambda (nm)');
grid on
axis tight
drawnow
end
0 Comments
Accepted Answer
Walter Roberson
on 25 Apr 2023
The OceanOptics programming manual is clear that the drivers are written in Java, and that
Therefore in order to read from two spectrometers at the same time, you would need more than one java thread.
You would certainly be able to create additional java threads by using Parallel Computing Toolbox with spmd or parfeval(). You might be able to create one java thread for each worker of a backgroundPool (background pools have some restrictions and I do not know whether they can deal with java.)
You just might maybe be able to split off java threads inside MATLAB https://www.javatpoint.com/how-to-create-a-thread-in-java and work with them. I recall from a number of years ago that people at least used to be successful in creating multiple java threads within MATLAB; the difficulty is in assigning the different tasks to different java threads; I do not know how that works.
More Answers (1)
Dhruv
on 25 Apr 2023
To record from two OceanOptics spectrometers simultaneously, try following the below steps:
- Create two instances of the instrument control toolbox for each spectrometer, and assign them to separate variables (e.g., spectrometer1 and spectrometer2).
- Retrieve the wavelengths of both spectrometers using the getWavelengths function.
- Create a figure to plot the spectra from both spectrometers.
- Use a loop to acquire data from both spectrometers simultaneously. Inside the loop, call the getSpectrum function on both spectrometers and store the returned data in separate variables (e.g., spectralData1 and spectralData2).
- Plot both spectra in the same figure using different line colors or styles to distinguish between them.
I hope that the steps outlined above will help you with the solution you are looking for.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!