How to shorten the code to convert information word to hamming code in MARIE - marie

I have made a MARIE code that takes the input of 8 information bits and converts it to 12-bit hamming code. How to shorten this code? I am using the same XOR function for each parity bit with different value of each bit.
I have first taken input for 8 bits in a loop and stored it at different addresses like an array. Then, I found the XOR of different parity bits (knowing the positions beforehand) and substituted the parity bits in the main information word (using the addresses of each position). Finally gave an output of the hamming code.
I have posted the code below:
'''
ORG 100
getInput, LoadI data
Skipcond 000
Input
Store temp
LoadI data
Skipcond 000
Load temp
StoreI data
Load data
Add one
Store data
Clear
Load size
Subt one
Store size
Skipcond 400
Jump getInput
Load dataTop
Add two
Store data
Load sizeMax
Store size
p1loop, LoadI data
Add p1
Store p1
Load data
Add two
Store data
Load size
Subt two
Store size
Skipcond 400
Jump p1loop
XOR, Load p1
Subt two
Store p1
Skipcond 000
Jump XOR
Add two
Store p1
Load dataTop
Store data
Load p1
StoreI data
Load dataTop
Add two
Store data
LoadI data
Store p2
Load sizeMax
Subt 2
Store size
p2loop, Load data
Add three
Store data
LoadI data
Add p2
Store p2
Load data
Add one
Store data
LoadI data
Add p2
Store p2
Load size
Subt four
Store size
Skipcond 400
Jump p2loop
XOR2, Load p2
Subt two
Store p2
Skipcond 000
Jump XOR2
Add two
Store p2
Load dataTop
Add one
Store data
Load p2
StoreI data
Load dataTop
Add four
Store data
Load three
Store size
p3loop, LoadI data
Add p3
Store p3
Load data
Add one
Store data
Load size
Subt one
Store size
Skipcond 400
Jump p3loop
Load dataTop
Add eleven
Store data
LoadI data
Add p3
Store p3
XOR3, Load p3
Subt two
Store p3
Skipcond 000
Jump XOR3
Add two
Store p3
Load dataTop
Add three
Store data
Load p3
StoreI data
Load dataTop
Add eight
Store data
Load eight
Store size
p4loop, LoadI data
Add p4
Store p4
Load data
Add one
Store data
Load size
Subt one
Store size
Skipcond 400
Jump p4loop
XOR4, Load p4
Subt two
Store p4
Skipcond 000
Jump XOR4
Add two
Store p4
Load dataTop
Add seven
Store data
Load p4
StoreI data
Load dataTop
Store data
Load sizeMax
Store size
ShowOutput, Clear
LoadI data
Output
Load data
Add one
Store data
Clear
Load size
Subt one
Store size
Skipcond 400
Jump ShowOutput
Halt
one, DEC 1
two, DEC 2
three, DEC 3
four, DEC 4
seven, DEC 7
eight, DEC 8
eleven, DEC 11
p1, DEC 0
p2, DEC 0
p3, DEC 0
p4, DEC 0
sizeMax, DEC 12
size, DEC 12
dataTop, HEX 1B0
temp, DEC 0
data, HEX 1B0
DEC -1
DEC -1
DEC 1
DEC -1
DEC 1
DEC 1
DEC 1
DEC -1
DEC 1
DEC 1
DEC 1
DEC 1
'''

Some small points:
XOR4, Load p4
Subt two
Store p4
Skipcond 000
Jump XOR4
Add two
Store p4
Don't need to both load & store p4 inside the loop, so this would also work:
Load p4
XOR4,
Subt two
Skipcond 000
Jump XOR4
Add two
Store p4
For another point, you don't really need p2 through p4, because when the code is done with p1, it never uses it again, instead switching to p2, finishes with that then moves on to p3.  The only benefit of having p2 through p4 is that they are initialized to 0.  So, the code could use just p1 everywhere as long as it is cleared before looping.
I mention that because one approach to making the code smaller is to share the same code for all 4 looping operations.  That would require some abstraction, and removing the unnecessary variables can help with that.
Otherwise, most of the various loops read the same except for the constants they use.  So, perhaps put the constants in a table, and then an outer loop of around the shared code so it runs 4 times, using different constants from the table.
The 4 loops do have some other differences, and perhaps those differences could also be selected from the table or perhaps by testing the outer loop variable, within some if-then type constructs in the inner loop.

Related

Marie JS to the power of

I need to write a code for raising x to the power of y in Marie.js. That's what I have for now, however it spits out an error SyntaxError:L41 - Unknown label Loop.
Input
Subt One
Store Count
Input
Store x
Store y
Jns Exp
Load Wyn
Output
End, Halt
Exp, Hex 0
Loop2, Load Count
Skipcond 800
JumpI Exp
JnS Multi
Load Wyn
Store x
Load Count
Subt One
Store Count
Jump Loop2
Multi, Hex 0
Load Zero
Store Wyn
Loop, Load x
Skipcond 800
JumpI Multi
Load Wyn
Add y
Store Wyn
Load x
Subt One
Store x
Jump Loop
x, Dec 2
y, Dec 3
Zero, Dec 0
One, Dec 1
Wyn, Dec 0
Count, Dec 0
I understand the necessity for 2 loops, which execute y-1 times, however Im completely clueless what I'm doing wrong.
You have to define a label before you can Jump to it; you have not defined the label Loop. You have defined a label named Loop2.

More efficient way to search a large matrix in MATLAB?

I have a code that does what I want but it is too slow because I have a very large mat file with a matrix (33 gigabyte) that I need to search for particular values and extract those.
The file that I'm searching has the following structure:
reporter sector partner year ave
USA all CAN 2007 0.060026126
USA all CAN 2011 0.0637898418
...
This goes on for millions of rows. I want to extract the last (5th) column value for particular reporter and partner values (sector and year are fixed). In actuality there are more fixed values that I have taken out for the sake of simplicity but this might slow down my code even more. The country_codes and partner values need to vary and are looped for that reason.
The crucial part of my code is the following:
for i = 1:length(country_codes)
for g = [1:length(partner)]
matrix(i,g) = big_file(...
ismember(GTAP_data(:,1), country_codes(i)) & ... % reporter
ismember(GTAP_data(:,2), 'all') & ...sector
ismember(GTAP_data(:,3), partner(g)) & ... partner
ismember([GTAP_data{:,4}]', 2011) & ... year
,5); % ave column
end
end
In other words, the code goes through the million rows and finds just the right value by applying ismember with logical & on everything.
Is there a faster way to do this than using ismember? Can someone assist?
So what I see is you build a big table out of the data in different files.
It seems your values are text-based. That takes up more memory. "USA" already takes up three bytes of memory. If you have less then 255 countries to concider, you could store them as only one byte in uint8 format.
If you can store all columns as a value between 0 and 255 you can make a uint8 matrix that can be indexed very fast.
As an example:
%demo
GTAP_regions={'USA','NL','USA','USA','NL','GB','NL','USA','Korea Republic of','GB','NL','USA','Korea Republic of'};
S=whos('GTAP_regions');
S.bytes
GTAP_regions requires 1580 bytes. Now we convert it.
GTAP_regions_list=GTAP_regions(1);
GTAP_regions_uint=uint8(1);
for ct = 2:length(GTAP_regions)
I=ismember(GTAP_regions_list,GTAP_regions(ct));
if ~any(I)
GTAP_regions_list(end+1)=GTAP_regions(ct);
else
GTAP_regions_uint(end+1)=uint8(find(I));
end
end
S=whos('GTAP_regions_list');
S.bytes
S=whos('GTAP_regions_uint');
S.bytes
GTAP_regions_uint we need to use to do indexing, and it is now only 10 bytes and will be very fast to analyse.
GTAP_regions_list we need to use to find what index value belongs to what country, is only 496 bytes.
You can also do this for sector, partner and year, depending on the range of years. If it is no more than 255 different years it will work. Otherwise you could store it as uint16 and have 65535 possible options.

Pick specific timepoints from a timeseries

I have a 164 x 246 matrix called M. M is data for time series containing 246 time points of 164 brain regions. I want to work on only specific blocks of the time series, not the whole thing. To do so, I created a vector called onsets containing the time onset of each block.
onsets = [7;37;82;112;145;175;190;220];
In this example, there are 8 blocks total (though this number can vary), each blocks containing 9 time points. So for instance, the first block would contain time point 7, 8, 9,..., 15; the second block would contain time point 37, 38, 39,..., 45. I would like to extract the time points for these 8 blocks from M and concatenate 8 these blocks. Thus, the output should be a 164 x 72 matrix (i.e., 164 regions, 8 blocks x 9 time points/per block).
This seems like a very simple indexing problem but I'm struggling to do this efficiently. I've tried indexing each block in M (for intance, vertcat(M(onsets(1,1):onsets(1,1)+8,:));) then use vertcat but this seems very clumsy. Can anyone help?
Try this:
% create sample data
M = rand(164,246);
% create index vector
idx = false(1,size(M,2));
onsets = [7;37;82;112;145;175;190;220];
for i=1:numel(onsets)
idx(onsets(i):onsets(i)+8) = true;
end
% create output matrix
MM = M(:,idx);
You seem to have switched the dimensions somehow, i.e. you try to operate on the rows of M whilst according to your description you need to operate on the columns. Hope this helps.

Matlab: Tempo-Alignment according to Timestamps

May be it is so simple but I'm new to Matlab and not good in Timestamps issues in general. Sorry!
I have two different cameras each contains timestamps of frames. I read them to two arrays TimestampsCam1 and TimestampsCam2:
TimestampsCam1 contains 1500 records and the timestamps are in Microseconds as follows:
1 20931160389
2 20931180407
3 20931200603
4 20931220273
5 20931240360 ...
and TimestampsCam2 contains 1000 records and the timestamps are in Milliseconds as follows:
1 28275280
2 28315443
3 28355607
4 28395771
5 28435935 ...
The first camera starts capturing first and ends a bit later than the second camera. So what I need to do is to know exactly where a frame from first camera is captured at the same time (or nearly the same time) by the other camera. In other words, I want to align the two arrays(cameras) in time according to the timestamps. I want to get at the end two arrays of same size where each record is tempo-aligned to the corresponding record in the other array.
Many thanks to all!
Sam
Make sure they are in the same unit of measurement, e.g. microseconds
Create an index which contains all values, except duplicates, suppose this one is 2400 records long
Create two NaN vectors of length 2400 by putting the value (for example the framenumber) at the place where the index matches the timestamp
Now you have two aligned vectors with NaNs to pad them where required.

iPhone - Text Displayed from Database when Buttons Pushed

I am new to the iPhone. I am writing an App that will display text information based on the sequence someone presses on a set of buttons. There are five (5) Tables that each have about sixty (60) responses that are about the size of the sample below: There will be a total of maybe three hundred (300) unique responses. I want to be able to update these 300 unique answers each 6 months or so in an updated version. What is the best storage and retrieval method? SQlite?, Core Data? some other method?
SAMPLE:
Hbts tb mhkx h fhvbrhblx nmprxssnbn hnd bx rxghrdxd
hs h spxcnhl pxrsbnhlnty. Ns thxrxfbrx cbnsthntly
bn thx whtch tb sxx whxthxr hx ns succxxdnng
nn thns hnd hbw bthxrs hrx rxhctnng tb hnm; thns
mhkxs hnm fxxl thht hx ns nn cbntrbl. Usxs thctncs
clxvxrly nn brdxr tb bbthnn nnfluxncx hnd spxcnhl rxcbgnntnbn.
Buwcxptnblx tb thx xsthxtnc br brngnnh*
You could do a plist or series of plists. 300 may not really warrant a database. And plists would be very simple to update.