Want to omit some values from TCP packet - matlab

I have successfully created the tcp connection between processing and matlab. I am using oscp5 library for that. I am sending an array containing 3 float values from processing.
But in Matlab, I am getting 5 extra values. Please advise how I can omit these values.
Please note that I can't use another library.
Also, someone suggest me, while creating message, I have assigned it a label "/test". When matlab's server gets the message, it will read this as well as the data in your package as assembled by oscP5. So, I have to teach matlab how to interpret that data. Hence, I need to go to the oscP5 website and review java docs and the source code to understand the packaging format. Here the link
http://www.sojamo.de/libraries/oscP5/
You can see the code below.
-------------Processing Code-------------
import oscP5.*;
import netP5.*;
OscMessage myMessage;
OscP5 oscP5tcpClient;
void setup() {
size(640, 360);
oscP5tcpClient = new OscP5( this, "141.44.219.204", 1234, OscP5.TCP);
}
void draw() {
background(255);
OscMessage myMessage = new OscMessage("test\");
myMessage.add(new float[] {123.2, 134.5, 52.5}); ==>> Sent Message
oscP5tcpClient.send(myMessage);
print(50+sin(a)*40.0);
}
-----------------------------------------------------------------
--------------------------MATLAB Code----------------------------
>> tcpipServer = tcpip('141.44.219.161',1234,'NetworkRole','Server');
>> fopen(tcpipServer)
>> data = fread(tcpipServer, 8 , 'float32')
data =
0 ===>> want to omit
0.0000 ===>> want to omit
0 ===>> want to omit
123.2000
134.5000
52.5000
0.0000 ===>> want to omit
0 ===>> want to omit
>>
-----------------------------------------------------------------

Related

Why when i encode the repeated field in nanopb with max_count, the bytes.written is zero(empty) in the other hands it cant be encoded

I'm using arduino for encoding the massage, i have tried for required and success for encoding and decoding back, but for repeated, after i encode it, the size of buffer is 0, so i cant send my buffer to other arduino
here is my code
file.ino
{
for(int i=0;i<7;i++)
message.header[i]=i+1;
//this is my variabel, i declare in .proto = repeated int32 header = 4 [(nanopb).max_count = 10, (nanopb).fixed_length = true];
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
bool status = pb_encode(&stream, Message_fields, &message);
Serial.println(stream.bytes_written);
//when i print this after encode, the data is loss, but when the field type is required, it will show some data bytes
}
Your header variable is fixed-length array of 10 entries. That should be ok. If it was not a fixed-length one there would be separate header_count field that you would have to set to the actual number of entries. You can look inside generated .pb.h to double-check that there is no header_count field.
Your code does not show the length of buffer you have allocated. Is it perhaps too short? Though that message should take only about 14 bytes.
You could also check whether status is true, i.e. whether encoding was successful. If it was not, you can find more information from stream.errmsg.

Methods for Exp-Golomb CodeWord Construction and Parsing

I am working with OpenH264 Codec. OpenH264 is using Exp-Golomb Coding for header related information. I have studied several websites and gathered a little information about Exp-Golomb Coding. OpenH264 uses 4 types of Exp-Golomb coding methods. They are:
Ue [When values are only Non-Negative quantity]
Te [when values are only 1 or 0]
Se [when values are both negative and positive quantity]
Me [when values a standard code map is defined for values]
I have learnt how to Construct or Parse by Method Ue.
Syntax Format for Exp-Golomb(Ue) = [M-Zeros][1][INFO].
Construction: Suppose We have a Code_Num = 226.
Now,
M = floor(log2(Code_Num)) = floor(log2(226)) = 7
INFO = Code_Num + 1 - pow(2,M) = 226 + 1 - 128 = 99 = (1100011) in Binary
So,
CodeWord = 0000000 1 1100011 [M-zeros, 1 ignoring bit, INFO]
Parsing:
Suppose We have a CodeWord = 000000011100011
Code_Num = pow(2,M) + INFO - 1 = 128 + 99 - 1 = 226
Now I can calculate Exp-Golomb(Ue). But I want to learn all the theories related Se, Te and Me. But I am unable to find any resources for other methods. Please help me.
OpenH264 is an implementation of the H.264/AVC video codec.
AVC uses Exp-Golomb coding in it's various headers, all compatible encoders have to as well.
Also, te(v) stands for Truncated Exponential-golomb encoding.
Anyway, you can find information about reading signed Exponential-Golomb codes on the wiki page:
but a real quick tl;dr is the 0 = 1, 1 = 010, -1 = 011, etc.
as for this mess:
M = floor(log2(Code_Num)) = floor(log2(226)) = 7
INFO = Code_Num + 1 - pow(2,M) = 226 + 1 - 128 = 99 = (1100011) in Binary
So,
CodeWord = 0000000 1 1100011 [M-zeros, 1 ignoring bit, INFO]
That's not at all accurate, you're supposed to add 1 during encoding, and subtract 1 during decoding (for unsigned Exp-Golomb only), Signed Exp-Golomb uses a completely different system.
Edit:
Mapped Exp-Golomb is exactly the same as Unsigned Exp-Golomb, plus a table lookup.
Truncated Exp-Golomb is the same as standard RICE aka Unary coding, except the stop bit is 0.
If you don't feel like creating your own decoders/encoders, take a look at my project BitIO, because I've already written them, especially ReadRICE/WriteRICE, and ReadExpGolomb/WriteExpGolomb functions, BitIO on Github

typecasting udp conversion issue

I am having a problem typecasting values that are being sent from my embedded board which is running a udp server to my matlab udp client which is not showing the correct value when I try to typecast the 4 bytes of data of the uint32 value. For example I send from the server a value(i.e. 1359151104), on the matlab side I receive this number : 1359200000. The code that I am using for this is :
each mssgt takes all the of each corresponding place of the 4 byte value and makes an array to make the array of all of the values since I am receiving 153 values total, otherwise I wouldnt do the outSet#(this is so that I get one big array with all of the values ).
outSet1 = uint32(mssgt(1:4:length(mssgt)-3));
outSet2 = uint32(mssgt(2:4:length(mssgt)-2));
outSet3 = uint32(mssgt(3:4:length(mssgt)-1));
outSet4 = uint32(mssgt(4:4:length(mssgt)));
outSet = uint32(2^0*outSet1 + 2^8*outSet2 + 2^16*outSet3 + 2^24*outSet4);
For some of the bytes, I have negative numbers, I had this implementation before the above code but it also doesnt work.
for mm = 1 : length(mssgt)
if mssgt(mm) < 0
mssg(mm) = int16(256) + int16(mssgt(mm));
else
mssg(mm) = int16(mssgt(mm));
end
end
I cant see what I am doing wrong. what is the error that is happening?

Does a function cache exist in Matlab?

In Python we have lru_cache as a function wrapper. Add it to your function and the function will only be evaluated once per different input argument.
Example (from Python docs):
#lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
>>> [fib(n) for n in range(16)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
>>> fib.cache_info()
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
I wonder whether a similar thing exists in Matlab? At the moment I am using cache files, like so:
function result = fib(n):
% FIB example like the Python example. Don't implement it like that!
cachefile = ['fib_', n, '.mat'];
try
load(cachefile);
catch e
if n < 2
result = n;
else
result = fib(n-1) + fib(n-2);
end
save(cachefile, 'result');
end
end
The problem I have with doing it this way, is that if I change my function, I need to delete the cachefile.
Is there a way to do this with Matlab realising when I changed the function and the cache has become invalidated?
Since matlab 2017 this is available:
https://nl.mathworks.com/help/matlab/ref/memoizedfunction.html
a = memoized(#sin)
I've created something like this for my own personal use: a CACHE class. (I haven't documented the code yet though.) It appears to be more flexible than Python's lru_cache (I wasn't aware of that, thanks) in that it has several methods for adjusting exactly what gets cached (to save memory) and how the comparisons are made. It could still use some refinement (#Daniel's suggestion to use the containers.Map class is a good one – though it would limit compatibility with old Matlab versions). The code is on GitHub so you're welcome to fork and improve it.
Here is a basic example of how it can be used:
function Output1 = CacheDemo(Input1,Input2)
persistent DEMO_CACHE
if isempty(DEMO_CACHE)
% Initialize cache object on first run
CACHE_SIZE = 10; % Number of input/output patterns to cache
DEMO_CACHE = CACHE(CACHE_SIZE,Input1,Input2);
CACHE_IDX = 1;
else
% Check if input pattern corresponds something stored in cache
% If not, return next available CACHE_IDX
CACHE_IDX = DEMO_CACHE.IN([],Input1,Input2);
if ~isempty(CACHE_IDX) && DEMO_CACHE.OUT(CACHE_IDX) > 0
[~,Output1] = DEMO_CACHE.OUT(CACHE_IDX);
return;
end
end
% Perform computation
Output1 = rand(Input1,Input2);
% Save output to cache CACHE_IDX
DEMO_CACHE.OUT(CACHE_IDX,Output1);
I created this class to cache the results from time-consuming stochastic simulations and have since used it to good effect in a few other places. If there is interest, I might be willing to spend some time documenting the code sooner as opposed to later. It would be nice if there was a way to limit memory use as well (a big consideration in my own applications), but getting the size of arbitrary Matlab datatypes is not trivial. I like your idea of caching to a file, which might be a good idea for larger data. Also, it might be nice to create a "lite" version that does what Python's lru_cache does.

how to get value of energy from inbuilt function graycoprops()

how to fetch value of energy from the code given below
g=rgb2gray(im);
g=double(g);
stats = graycoprops(g, {'energy'});
disp(stats)
it shows result like this
Energy: 1.4492e-005
but i want only 1.4492e-005
so that i can store it into a file OR
is there any way to store stats variable i.e 'Energy: 1.4492e-005'
into file. i tried this one
stats = graycoprops(g, {'energy'});
fprintf(fwener,'%s',stats);
it gives me error "??? Undefined function or variable 'fwener'."
stats is a structure.
stats.Energy
should give you the number you want to save in a file.
As Molly said, stats is structure. If you do this
disp(stats)
disp('List of variables');
whos
disp('List of fields');
fieldnames(stats)
you will see this:
Energy: 3.7247e-006
List of variables
Name Size Bytes Class Attributes
ans 1x1 124 cell
g 450x600 2160000 double
im 450x600x3 810000 uint8
stats 1x1 184 struct
List of fields
ans =
'Energy'
So you should check little deeper what MatLab says.
Have fun ;o)