OpenViBE forum

The OpenViBE community
It is currently Sat Dec 16, 2017 8:26 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Apr 20, 2017 12:29 pm 
Offline

Joined: Sun Apr 16, 2017 6:10 am
Posts: 1
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:
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:
File comment: OpenVibe scenario
Untitled picture.png
Untitled picture.png [ 33.07 KiB | Viewed 358 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue May 02, 2017 2:35 pm 
Offline

Joined: Tue Dec 04, 2012 3:53 pm
Posts: 601
Location: INRIA Rennes, FRANCE
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Americanized by Maƫl Soucaze.