Clear Filters
Clear Filters

How do I record from two spectrometers simultaneously

4 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 25 Apr 2023
The OceanOptics programming manual is clear that the drivers are written in Java, and that
  • 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.
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.
  1 Comment
Robert
Robert on 25 Apr 2023
This seems like the best area to explore going forward
I'll have a look into creating and assigning separate threads, but may be limited by how easy it is to reliably set and assign to individual different threads

Sign in to comment.

More Answers (1)

Dhruv
Dhruv on 25 Apr 2023
To record from two OceanOptics spectrometers simultaneously, try following the below steps:
  1. Create two instances of the instrument control toolbox for each spectrometer, and assign them to separate variables (e.g., spectrometer1 and spectrometer2).
  2. Retrieve the wavelengths of both spectrometers using the getWavelengths function.
  3. Create a figure to plot the spectra from both spectrometers.
  4. 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).
  5. 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.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!