Multiple Inputs/Outputs into Matlab

Concerning processing components: filters, file load/save, visualizations, communication ...
Post Reply
zzoyelam
Posts: 1
Joined: Sun Apr 16, 2017 6:10 am

Multiple Inputs/Outputs into Matlab

Post by zzoyelam »

Hi all,

I am working on a senior design project for school, and I am using g.MobiLab+, Openvibe, and Matlab. I've gone through the tutorials and I am still unclear about how OpeVibe interfaces with multiple/different inputs and outputs when writing the Matlab script. I have essentially tried to get this working on the Tutorial 2 code, but it seems to fail every time. I've attached my scenario.Any assistance on the issue would be very helpful. Thanks!

Code: Select all

function box_out = EEG_Process(box_in)
    for i = 1: OV_getNbPendingInputChunk(box_in,1)
        if(~box_in.user_data.is_headerset)
            % The output is the input + noise, only on first channel
            box_in.outputs{1}.header = box_in.inputs{1}.header;
            box_in.outputs{1}.header.n1_channels = 1;
            box_in.outputs{1}.header.channel_names = {'Channel 1 '};
            box_in.user_data.is_headerset = 1;


            % We print the header in the console
            disp('Input header is :')
            box_in.inputs{1}.header
            disp('Output header is :')
            box_in.outputs{1}.header

        end
        
        % we increment the matrix count
		box_in.user_data.n1_matrix_processed = box_in.user_data.n1_matrix_processed + 1;
		[box_in, start_time, end_time, matrix_data] = OV_popInputBuffer(box_in,1
        
		% Sample Frequency
        Fs = box_in.inputs{1}.header.sampling_rate;
        
        % Sample time
		T = 1/Fs;
        
        % Length of signal
		L = box_in.inputs{1}.header.nb_samples_per_buffer;
        
        % Time vector
		t = (0:L-1)*T;                                      
		
		%{
        We generate the requested sinusoid noise
		noise_amplitude = box_in.settings(2).value;
		noise_frequency = box_in.settings(1).value;
		sinusoid = sin(2*pi* noise_frequency *t);
        %}
		
        
		% We add this sinus to the original signal on first channel only
		sig = matrix_data(1,1:L);
		%{
        %subplot(2,1,1);
		%plot(Fs*t,sinusoid)
		%title('Noise')
		%xlabel('time (seconds)')
		
		%subplot(3,1,2);
		%plot(Fs*t,sig)
		%title('Signal Corrupted with the noise (channel 1)')
		%xlabel('time (seconds)')
		
		NFFT = 2^nextpow2(L); % Next power of 2 from length of y
		Y = fft(sig,NFFT)/L;
		f = Fs/2*linspace(0,1,NFFT/2+1);

		% Plot single-sided amplitude spectrum.
		fmin = box_in.settings(1).value;
		fmax = box_in.settings(2).value;
		%subplot(3,1,3)
		plot(f(fmin*2:fmax*2),2*abs(Y(fmin*2:fmax*2))) 
		title('Single-Sided Amplitude Spectrum of EEG Signal')
		xlabel('Frequency (Hz)')
		ylabel('Amplitude')
		%}
		% we sum the FFT for later mean computation
		if box_in.user_data.nb_matrix_processed == 1 
			box_in.user_data.mean_fft_matrix = Y;
		else
			box_in.user_data.mean_fft_matrix = box_in.user_data.mean_fft_matrix + Y;
		end
		
		box_in = OV_addOutputBuffer(box_in,1,start_time,end_time,sig);
	end
           

    for i = 1: OV_getNbPendingInputChunk(box_in,2)
        if(~box_in.user_data.is_headerset)
            % The output is the input + noise, only on first channel
            box_in.outputs{2}.header = box_in.inputs{2}.header;
            box_in.outputs{2}.header.nb_channels = 2;
            box_in.outputs{2}.header.channel_names = {'FFT '};
            box_in.user_data.is_headerset = 1;


            % We print the header in the console
            disp('Input header is :')
            box_in.inputs{2}.header
            disp('Output header is :')
            box_in.outputs{2}.header

        end
        % we increment the matrix count
		box_in.user_data.n2_matrix_processed = box_in.user_data.n2_matrix_processed + 1;
		
		[box_in2, start_time, end_time, matrix_data2] = OV_popInputBuffer(box_in,2);
        
		Fs = box_in.inputs{2}.header.sampling_rate;        % Sampling frequency
		T = 1/Fs;                                           % Sample time
		L = box_in.inputs{2}.header.nb_samples_per_buffer; % Length of signal
		t = (0:L-1)*T;                                      % Time vector
		
		%{
        We generate the requested sinusoid noise
		noise_amplitude = box_in.settings(2).value;
		noise_frequency = box_in.settings(1).value;
		sinusoid = sin(2*pi* noise_frequency *t);
        %}
		
        
		% We add this sinus to the original signal on first channel only
		sig = matrix_data(1:6,1:L);
		%subplot(2,1,1);
		%plot(Fs*t,sinusoid)
		%title('Noise')
		%xlabel('time (seconds)')
		
		%subplot(3,1,2);
		%plot(Fs*t,sig)
		%title('Signal Corrupted with the noise (channel 1)')
		%xlabel('time (seconds)')


		% Plot single-sided amplitude spectrum.
		fmin = box_in.settings(1).value;
		fmax = box_in.settings(2).value;
        
		%subplot(3,1,3)
		plot(f(fmin*2:fmax*2),2*abs(Y(fmin*2:fmax*2))) 
		title('Single-Sided Amplitude Spectrum of EEG Signal')
		xlabel('Frequency (Hz)')
		ylabel('Amplitude')
		
		% we sum the FFT for later mean computation
        % Very first data group that comes through
		if box_in.user_data.nb_matrix_processed == 1 
			box_in.user_data.mean_fft_matrix = Y;
        else
            % Remaining data that comes through
			box_in.user_data.mean_fft_matrix = box_in.user_data.mean_fft_matrix + Y;
		end
		
		box_in = OV_addOutputBuffer(box_in,2,start_time,end_time,sig);
	end
            
  box_out = box_in;
  box_out2 = box_in2;
end
Attachments
OpenVibe scenario
OpenVibe scenario
Untitled picture.png (33.07 KiB) Viewed 2561 times

jtlindgren
Posts: 775
Joined: Tue Dec 04, 2012 3:53 pm
Location: INRIA Rennes, FRANCE

Re: Multiple Inputs/Outputs into Matlab

Post by jtlindgren »

Hi Zzo,

I'd recommend pruning the script to smallest possible for clarity and then get it to work step by step. Each step should work and do something you understand.

Few helpful tips after a quick glance at your question & the attachments,

1) In openvibe, the input and output sockets of the boxes denote streams. Streams can have different contents. In the case of signal, the stream contents will be matrixes. The matlab box for a single signal input will then receive matrixes oriented [nChannels x nSamples] from the socket, where nSamples is dictated by the used chunk size. So to access individual channels of a single input stream or output, you need to work with such matrices.

2) I see a loop in your scenario; this is best avoided unless you know what you're doing, and even then something might go bonkers. Instead, try to make the scenario as a strictly acyclic graph.

Best,
Jussi

Post Reply