Understanding IDAT, reading DEFLATE's dynamic Huffman tree - png

I am looking to better understand zlib, deflate, and PNG encoding. With that said, I am having trouble applying the specifications of RFC-1950 and RFC-1951 to the IDAT portion of a PNG.
Below is binary for an image.
The example image is 50x50 and all pixels are RGB(255,0,0).
Okay, after the IDAT, expect two bytes specified by zlib; the CMF [0000029] then the FLG [000002a] (no FLG.FDICT). As expected the hex is 78DA which means use DEFLATE, 32K window, and a high level of compression.
Starting with the least-significant bit of the next byte, there will be three bits for the DEFLATE header, one bit to specify the last block and two bits for compression type (none, fixed, or dynamic) [000002b].
To read bits see this.
0000024: 01000011 01001001 01000100 01000001 01010100 01111000 CIDATx
000002a: 11011010 11101101 11001111 00110001 00010001 00000000 ...1..
0000030: 00000000 00001000 00000000 10100001 11101111 01011111 ....._
0000036: 01011010 00110011 10111000 01111010 00001100 00000100 Z3.z..
000003c: 10100000 10101001 11111001 00100000 00010001 00010001 ... ..
0000042: 00010001 00010001 00010001 00010001 00010001 00010001 ......
0000048: 00010001 00010001 00010001 00010001 00010001 00010001 ......
000004e: 00010001 00010001 00010001 00010001 00010001 00010001 ......
0000054: 00010001 00010001 00010001 00010001 00010001 00010001 ......
000005a: 00010001 00010001 00010001 00010001 00010001 00010001 ......
0000060: 00010001 00010001 00010001 00010001 00010001 10010001 ......
0000066: 10001011 00000101 10110000 00110011 01110101 10010110 ...3u.
000006c: 01111001 11000101 00011100 10110001 00000000 00000000 y.....
0000072: 00000000 00000000 01001001 01000101 01001110 01000100 ..

The next three bits are from the bottom of 11101101. The 101 is 10 for a dynamic block and 1 indicating that this is the last block. That starts a 61-byte deflate stream, which decodes to the following (as disassembled by infgen):
last
dynamic
litlen 0 2
litlen 255 4
litlen 256 4
litlen 274 4
litlen 283 4
litlen 285 1
dist 3 1
dist 15 1
literal 0 255 0 0 255
match 196 4
literal 0
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 258 201
match 44 4
end
The first decompressed byte is a zero, and decompresses to 10,050 bytes.

Related

In KDB, how do I sum the previous 3 numbers in a list?

Say I have a list of numbers:
j: (til 40)*9
0 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252 261 270 279 288 297 306 315 324 333 342 351
What's the most elegant way to get the sum of the previous 3 (or n) numbers in the list? (Ideally considering large RAM constrained lists).
Does this work?
q)3 msum j
0 9 27 54 81 108 135 162 189 216 243 270 297 324 351 378 405 432 459 486 513 ..

how to decode raw IR data using lirc on raspberry pi 3

I've been trying to decode my IR raw data but always getting unknown code. i used
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define SHORT 600
#define LONG 300
#define MARGIN 200
#define INTRO 3500
#define INTRO2 1700
#define SEPARATOR 9880
char decode(int val1, int val2){
if (abs(val1 - INTRO) < MARGIN && abs(val2 -
INTRO2) < MARGIN)
return '[';
if (abs(val1 - SHORT) < MARGIN && abs(val2 -
SHORT) < MARGIN)
return '0';
if (abs(val1 - SHORT) < MARGIN && abs(val2 -
LONG) < MARGIN)
return '1';
if (abs(val1 - SHORT) < MARGIN && abs(val2 -
SEPARATOR) < MARGIN)
return ']';
}
int abs(int val){
if (val < 0)
return -val;
else
return val;
}
int main(int argc, char** argv){
if (argc < 3){
printf("usage = %s fileName output\n",
argv[0]);
exit(0);
}
int val1, val2;
char delims[] = " ";
char *result = NULL;
FILE* f = fopen(argv[1],"r");
FILE* out = fopen(argv[2],"w");
char line[256];
int index = 0;
char* nIndex = 0;
while (fgets(line, 256, f) != NULL){
result = strtok( line, delims );
index = 0;
while( result != NULL ) {
//printf( "result[%d] is \"%s\"\n",
index, result );
if (index & 1){
if (nIndex = strchr(result,'\n')){
//printf("New line detected in result\n");
*nIndex = '\0';
}
val2 = atoi(result);
char dec = decode(val1,val2);
fprintf(out,"%c",dec);
}else{
val1 = atoi(result);
}
result = strtok( NULL, delims );
index++;
}
//printf (line);
}
fprintf(out, "]\n");
fclose(f);
fclose(out);
}
and my Raw data are
3596 1598 574 298 574 1166
572 301 603 268 576 295
573 298 600 271 610 260
577 295 572 299 573 298
573 299 603 268 577 1164
574 298 574 296 573 299
543 329 575 295 571 301
573 298 569 1173 576 1165
546 1197 662 184 636 261
544 1198 600 271 573 299
573 297 574 297 544 328
605 266 573 299 544 326
573 298 605 266 575 296
576 296 543 327 575 296
604 268 575 297 515 356
599 272 572 298 601 271
571 299 574 301 573 295
576 295 573 279 561 329
545 325 546 325 573 300
573 297 573 1169 572 1170
574 298 544 327 575 296
547 325 511 359 603 76167
3599 1625 548 322 553 1189
552 321 523 348 550 321
551 320 517 354 574 297
551 320 551 321 577 293
547 325 550 321 521 1219
525 348 552 319 550 321
549 322 552 320 549 322
550 321 548 1193 551 1191
584 1159 518 353 549 322
580 1170 546 317 551 321
552 319 579 292 551 320
552 320 551 320 553 318
549 322 579 292 547 324
550 321 555 317 550 320
581 1160 587 286 543 1198
552 1190 552 1191 550 321
551 321 581 290 547 324
549 322 550 321 548 1193
552 1191 550 319 583 292
550 320 576 295 579 292
551 320 519 353 551 319
548 323 553 1189 522 1220
550 1192 550 1192 551 1192
523 1219 551 1191 451 1291
522 350 550 321 553 318
548 323 555 316 551 320
553 319 552 319 554 316
584 300 566 294 550 320
552 319 549 322 550 321
553 321 552 317 549 321
550 1192 523 1219 550 1192
558 314 549 322 552 319
551 321 552 318 552 320
551 320 549 322 551 320
550 1192 548 1194 550 1192
551 320 613 258 526 346
554 319 549 322 522 348
576 294 522 350 522 348
555 317 523 348 552 319
552 319 550 321 548 323
552 319 580 1161 551 322
553 318 551 320 549 322
608 263 582 289 525 1216
550 322 521 350 552 320
551 320 552 319 549 322
581 290 551 321 550 320
552 319 555 317 523 348
548 324 551 319 546 325
551 321 479 391 572 1169
551 1192 554 1188 555 1188
551 321 549 1191 557 1187
551
and i got
[1�11111111111�1111111��#11#111111111111111111111111111111��11111�[1
11111111111+1111111##�11�11111111111111�1###111111##111111111
,###+#s111111111111111111#+#111111111###1111111111111111�111111(11111111111111111�#
please help me, thank you!
I am not used to playing with IR, but ...
If you want to use lirc you should configure it : http://lirc.org/html/configuration-guide.html.
You do not need to develop a specific utility to decode data.
You can use mode2 to get output form driver http://www.lirc.org/html/mode2.html
mode2 --driver default --device /dev/lirc0
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
pulse 2750
space 800
pulse 500
space 350
pulse 550
space 350
pulse 550
Then lircd.conf is used to map data to key http://www.lirc.org/html/lircd.conf.html and with irw you get the result :
$ irw
000000037ff07bef 00 KEY_VOLUMEUP Acer_Aspire_6530G_MCE
000000037ff07bef 01 KEY_VOLUMEUP Acer_Aspire_6530G_MCE
000000037ff07bdd 00 KEY_ENTER Acer_Aspire_6530G_MCE
000000037ff07bdd 02 KEY_ENTER Acer_Aspire_6530G_MCE
It looks like you may be reading a remote intended for a Mitsubishi air conditioning system. Unlike most remotes, which just send a short signal corresponding to the button pressed and rely on the device being controlled to remember its previous state, Mitsubishi decided to transmit the entire desired state of the machine -- all the mode, fan, vane, and other settings get sent Every Time, as an 18-byte data block with checksum (which the remote then repeats once, as insurance against optical noise).
If you websearch "Mitsubishi minisplit IR protocol", you'll find some pages where people have at least partially analyzed this protocol.
This isn't the kind of approach LIRC's higher-level tools were designed to handle. You can still use the LIRC drivers, but they have to be accessed in "raw" mode and separate tools are used to interpret incoming signal or generate outgoing signal.
Searching Github finds libraries/tools others have written to work with this protocol. I haven't found one in a language I actually like working with, though, so I'm assuming I'm going to have to access the LIRC api calls to read/write the data and interpret/generate it myself.
RELATED: If you're using the ir-ctl program to do basic "raw" reading and writing while you test your hardware, be aware that ir-ctl's transmit mode has a line-length limit which this protocol sometimes exceeds if you put it all on a single line of text. The workaround is to insert line breaks every so often, so no single line of the input exceeds that hardcoded limit. (Yes, I've filed a bug report.)

RMarkdown: Creating two side-by-side heatmaps with full figure borders using the pheatmap package

I am writing my first report in RMarkdown and struggling with specific figure alignments.
I have some data that I am manipulating into a format friendly for the package pheatmap such that it produces heatmap HTML output. The code that produces one of these looks like:
cleaned_mayo<- cleaned_mayo[which(cleaned_mayo$Source=="MayoBrainBank_Dickson"),]
# Segregate data
ad<- cleaned_mayo[which(cleaned_mayo$Diagnosis== "AD"),-c(1:13)]
control<- cleaned_mayo[which(cleaned_mayo$Diagnosis== "Control"),-c(1:13)]
# Average data across patients and assign diagnoses
ad<- as.data.frame(t(apply(ad,2, mean)))
control<- as.data.frame(t(apply(control,2, mean)))
ad$Diagnosis<- "AD"
control$Diagnosis<- "Control"
# Combine
avg_heat<- rbind(ad, control)
# Rearrange columns
avg_heat<- avg_heat[,c(32, 1:31)]
# Mean shift all expression values
avg_heat[,2:32]<- apply(avg_heat[,2:32], 2, function(x){x-mean(x)})
#################################
# CREATE HEAT MAP
#################################
# Plot average heat map
pheatmap(t(avg_heat[,2:32]), cluster_col= F, labels_col= c("AD", "Control"),gaps_col = c(1), labels_row = colnames(avg_heat)[2:32],
main= "Mayo Differential Expression for Genes of Interest: Averaged Across \n Patients within a Diagnosis",
show_colnames = T)
Where the numeric columns of cleaned_mayo look like:
C1QA C1QC C1QB LAPTM5 CTSS FCER1G PLEK CSF1R CD74 LY86 AIF1 FGD2 TREM2 PTK2B LYN UNC93B1 CTSC NCKAP1L TMEM119 ALOX5AP LCP1
1924_TCX 1101 1392 1687 1380 380 279 198 1889 6286 127 252 771 338 5795 409 494 337 352 476 170 441
1926_TCX 881 770 950 1064 239 130 132 1241 3188 76 137 434 212 5634 327 419 292 217 464 124 373
1935_TCX 3636 4106 5196 5206 1226 583 476 5588 27650 384 1139 1086 756 14219 1269 869 868 1378 1270 428 1216
1925_TCX 3050 4392 5357 3585 788 472 350 4662 11811 340 865 1051 468 13446 638 420 1047 850 756 616 1008
1963_TCX 3169 2874 4182 2737 828 551 208 2560 10103 204 719 585 499 9158 546 335 598 593 606 418 707
7098_TCX 1354 1803 2369 2134 634 354 245 1829 8322 227 593 371 411 10637 504 294 750 458 367 490 779
ITGAM LPCAT2 LGALS9 GRN MAN2B1 TYROBP CD37 LAIR1 CTSZ CYTH4
1924_TCX 376 649 699 1605 618 392 328 628 1774 484
1926_TCX 225 381 473 1444 597 242 290 321 1110 303
1935_TCX 737 1887 998 2563 856 949 713 1060 2670 569
1925_TCX 634 1323 575 1661 594 562 421 1197 1796 595
1963_TCX 508 696 429 1030 355 556 365 585 1591 360
7098_TCX 418 1011 318 1574 354 353 179 471 1471 321
All of this code is wrapped around the following header in the RMarkdown environment: {r heatmaps, echo=FALSE, results="asis", message=FALSE}.
What I would like to achieve is the two heatmaps side-by-side with black boxes around each individual heat map (i.e. containing the title and legend of the heatmap as well).
If anyone could tell me how to do this, or either one individually it would be greatly appreciated.
Thanks!

Saving (in a matrix) the elapsed time and number of iterations for a large number of cases

I have a program that outputs the number of iterations and a test value, given inputs A1,A2,A3,A4.
I want to run through 5 values each of A1, A2, A3, A4, thus making 625 runs. In the process, I want to save the time elapsed for each run, the number of iterations, and test value in 3 separate matrices.
I have tried using 4 nested for loops, and made progress, but need some help on indexing the elements of the matrices. The iterator variables in the for loops don't match the indexing variables...
The code for the 4 nested loops is below:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(5,length(m)*length(m)*length(m));
for A1 = m
for A2 = m
for A3 = m
for A4 = n
tic
SmallMAX(A1,A2,A3,A4)
toc;
for i=1:numel(eltime)
for j = 1:length(n)
eltime(j,i) = toc;
end
end
end
end
end
end
The code for the main program is excerpted below:
function [k,test] = SmallMAX(A1,A2,A3,A4)
...
end
Thanks for any help.
In your case, the easiest way is to use A1, A2, A3 and A4 as counters instead of the actual values. This way you them to index the entries of eltime. We can then easily calculate the index in the second dimension with sub2ind and use A4 to index the first dimension of eltime. We need to adjust the arguments in SmallMAX as well.
Here is the code of the proposed method:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(length(n),length(m)*length(m)*length(m));
res_k = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
res_test = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
for A1 = 1:length(m)
for A2 = 1:length(m)
for A3 = 1:length(m)
for A4 = 1:length(n)
ind = sub2ind([length(m),length(m),length(m)],A3,A2,A1);
tic
[k,test] = SmallMAX(m(A1),m(A2),m(A3),n(A4));
eltime(A4,ind) = toc;
res_k(A4,ind) = k;
res_test(A4,ind) = test;
end
end
end
end
This is the order of the addressed entries of eltime:
eltime_order =
Columns 1 through 18
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86
2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87
3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88
4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
Columns 19 through 36
91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176
92 97 102 107 112 117 122 127 132 137 142 147 152 157 162 167 172 177
93 98 103 108 113 118 123 128 133 138 143 148 153 158 163 168 173 178
94 99 104 109 114 119 124 129 134 139 144 149 154 159 164 169 174 179
95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180
Columns 37 through 54
181 186 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266
182 187 192 197 202 207 212 217 222 227 232 237 242 247 252 257 262 267
183 188 193 198 203 208 213 218 223 228 233 238 243 248 253 258 263 268
184 189 194 199 204 209 214 219 224 229 234 239 244 249 254 259 264 269
185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270
Columns 55 through 72
271 276 281 286 291 296 301 306 311 316 321 326 331 336 341 346 351 356
272 277 282 287 292 297 302 307 312 317 322 327 332 337 342 347 352 357
273 278 283 288 293 298 303 308 313 318 323 328 333 338 343 348 353 358
274 279 284 289 294 299 304 309 314 319 324 329 334 339 344 349 354 359
275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360
Columns 73 through 90
361 366 371 376 381 386 391 396 401 406 411 416 421 426 431 436 441 446
362 367 372 377 382 387 392 397 402 407 412 417 422 427 432 437 442 447
363 368 373 378 383 388 393 398 403 408 413 418 423 428 433 438 443 448
364 369 374 379 384 389 394 399 404 409 414 419 424 429 434 439 444 449
365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450
Columns 91 through 108
451 456 461 466 471 476 481 486 491 496 501 506 511 516 521 526 531 536
452 457 462 467 472 477 482 487 492 497 502 507 512 517 522 527 532 537
453 458 463 468 473 478 483 488 493 498 503 508 513 518 523 528 533 538
454 459 464 469 474 479 484 489 494 499 504 509 514 519 524 529 534 539
455 460 465 470 475 480 485 490 495 500 505 510 515 520 525 530 535 540
Columns 109 through 125
541 546 551 556 561 566 571 576 581 586 591 596 601 606 611 616 621
542 547 552 557 562 567 572 577 582 587 592 597 602 607 612 617 622
543 548 553 558 563 568 573 578 583 588 593 598 603 608 613 618 623
544 549 554 559 564 569 574 579 584 589 594 599 604 609 614 619 624
545 550 555 560 565 570 575 580 585 590 595 600 605 610 615 620 625

Multidimensional scaling matrix error

I'm trying to use multidimensional scaling in Matlab. The goal is to convert a similarity matrix to scatter plot (in order to use k-means).
I've got the following test set:
London Stockholm Lisboa Madrid Paris Amsterdam Berlin Prague Rome Dublin
0 569 667 530 141 140 357 396 570 190
569 0 1212 1043 617 446 325 423 787 648
667 1212 0 201 596 768 923 882 714 714
530 1043 201 0 431 608 740 690 516 622
141 617 596 431 0 177 340 337 436 320
140 446 768 608 177 0 218 272 519 302
357 325 923 740 340 218 0 114 472 514
396 423 882 690 337 272 114 0 364 573
569 787 714 516 436 519 472 364 0 755
190 648 714 622 320 302 514 573 755 0
I got this dataset from the book Modern Multidimensional Scaling (Borg & Groenen, 2005). Tested it in SPSS using the PROXSCAL MDS method and I get the same result as stated in the book.
But I need to use MDS in Matlab in order to speed up the process. The tutorial on the site: http://www.mathworks.nl/help/stats/multidimensional-scaling.html#briu08r-4 looks the same as what I'm using above. When I change the data set as what is displayed above and run the code I get the following error: "Not a valid dissimilarity or distance matrix.".
I'm not sure what I'm doing wrong, and if classical MDS is the right choice. I also miss the possibility to say that I want the result in three dimensions (this will be needed in a later stage).
Your matrix is not symetric, check the indices (9,1) and (1,9). To quickly find asymetric indices use [x,y]=find(~(D'==D))