Thilina's Blog

I might be wrong, but…

Streaming data from MATLAB-workspace to Simulink

Since we are having our final year project term in the university we are working in a rush with the projects. Our final year project is related to a hardware implementation related to image processing; we use MATLAB together with Xilinx for simulations. This is something I did to make our final year project simulations easy. In this article I share my experience on how to send workspace data to MATLAB simulink with respect to a clock and data enable flag generated by simulink blocks.

MATLAB simulink block “from workspace” allows us to send workspace variables to simulink. The data format must be with at-least two column matrix since the first column is considered as the time stamp. To get a basic idea about how “from workspace” block works, let’s use following simulink model.

clip_image002

clip_image004

The parameter Data defines the data input from MATLAB workspace. You can rename it according to your variable. For my cases I do not use data interpolation, and zero-crossing detection. The last parameter has 4 options as below. I use ‘holding final value’ for my simulations.

clip_image006

As a test lets define a variable

data = [5 2; 6 4; 7 8];

and run them in these 4 modes.

Case 1: Extrapolation

Interpolate data checked

clip_image008

In this case all points are defined by extrapolation/interpolation of data.

Case 2: Setting to Zero

Interpolate data un-checked

clip_image010

In this case pre-defined points got its defined value and zero is elsewhere and final point is missing.

Case 3: Holding final Value

Interpolate data un-checked

clip_image012

In this case all defined points got its corresponding value and final value is kept after evaluating final defined point.

Now by using case 2 and 3 we can send serial data to simulink. But the question is how to send data when it was requested by an internal model and to control by internal clocking. Let’s try this by typing

data = [5 1 2 3 4];

in command prompt with the mode “Holding final Value”.

clip_image014

Now we got all data available after t = 5; That is the first element of the data[]. By using this method we can design a method to control data output. Matrix index selection can be controlled using a matlab function box using a simple code as below.

function dataout = fcn(source, index, max)
  if( index<=max && index>0 )
    dataout = source(index);
  else
    dataout = 0;
  end

This index can be controlled by a counter found in DSP block set. And data enable can be control by an AND operation to the clock input to the counter block.

clip_image016

Now the results as expected.

clip_image018

But at the case of self feed back in simulink it will give an error. To remove this error we can use unit delay block before and block.

clip_image020

clip_image021

For use this apparatus in more general case I modified my apparatus as below. This model was used to convert image into its FFT in our final year project. In this model we used Xilinx Block set to simulate a Verilog file in MATLAB using black box. Clocking is used by counter of Xilinx block set, the EDK processor is used since it requires to run the counter.

clip_image023

In this model we input 128×128 image, take its FFT. For this first we need to convert image into a stream and next send through the block set and convert it back to 128×128 array. To perform these operations I use 2 matlab scripts as below. First one converts image in to a stream and next convert the stream back to a 128×128 array.

To use this system first you have to run the serial converter script to update variables in to the workspace. Then run the simulink model to perform required operations on simulink and finally the reshape matrix script to convert the data back to image format.

Serial Converter

clc;
clear all;
close all;
img = cast(rgb2gray(imread('img.jpg')),'double');
[r c] = size(img);
len = r*c;
maxCnt = [0 len+1];
serialImage = reshape(img,1,len);
im_in = [0 serialImage];

Reshape Martix

index = find(dataAvb>0,1,'first');
limitV = index + (r*c)-1;
realData = realD(index:limitV)';
realData = reshape(realData,r,c);
imgData = imaginary(index:limitV)';
imgData = reshape(imgData,r,c);
xpt = find(rden>0,1,'first');
imm = imouT((xpt):(xpt)+len-1);
imm = imm';
img= reshape(imm,r,c);
img = cast(img,'uint8'); imshow(img);

The variable maxCnt defines the length to run and other data stored to the workspace defines the second script to how to extract data. Hope you got some idea to modify workspace data into our needs. Thank you very much for reading.

2011 July 1 - Posted by | FPGA, Image Processing, MATLAB

6 Comments »

  1. Its really a applaudable effort u are taking to share your project experience through ur blog.

    congrats and thanks 4 ur effort…

    Comment by sharon | 2011 August 3 | Reply

    • Thank you for the comment🙂

      Comment by Thilina S. | 2011 August 13 | Reply

  2. hi
    Its good material.
    if i want to input video input by taking frame by frame, how i can use your block.

    Comment by shamim | 2011 October 11 | Reply

  3. hello its really very help for those who starts up. thanks a lot for sharing your exp and knowledge in your block.

    Comment by Deepa | 2012 May 5 | Reply

  4. hi i m working on progrmmin, from a embedded function block1, the outputs are sensed instantaneously as scalars.. minimum of three outputs have to be compared by another embbedded fn block2.. instantaneous scalars from block1 have to be changed to vector and given as vector input to block2… how scalars can be stored as vectors…?

    Comment by rinky | 2012 October 27 | Reply

  5. Hi, I think your blog might be having browser compatibility issues.
    When I look at your website in Firefox, it looks
    fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to
    give you a quick heads up! Other then that,
    amazing blog!

    Comment by gold.preciousmetalsauthority1.com | 2013 July 4 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: