Related
I have the following function
function permutation(a){
let res = [];
for(let i=0; i<a.length; i++){
let restA = a.slice(0,i).concat(a.slice(i+1));
let rest = permutation(restA);
if(rest.length === 0){
res.push([a[i]]);
}else{
for(let j=0; j<rest.length; j++){
res.push([a[i]].concat(rest[j]));
}
}
}
return res;
}
which generates all permutations of 1D array with unique values like [1,2,3,4].
I also have a 2D array filled with zeroes and ones like this
[
[1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1]
]
I need to generate all unique permutations of this array. How do I do this?
You can ignore the fact it's a 2D array and consider this a 64 bit value with 34 1's and 30 0's. With every valid combination of these bits you can build a unique 2D array (8 bits per row).
However, if you do the math you'd see it's not really feasible to create all these combinations:
64! / ( 34! * 30!) = 1.620288e+18
This is almost as large as unsigned 64 bit range (1.8e+19) - it's practically a 10th of that.
Having said that, if you really want to do this, just iterate a 64 bit unsigned integer from 0 up to MAXVALUE, and test "on" bit count in each iteration - if it's 34, you've found a valid combination.
I am looking for a simple way to convert from c-code hex array to matlab
Available C-Code Format:
const uint16_t AUDIO_SAMPLE[] = {0x4952, 0x4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1}
Required Matlab Format:
AUDIO_SAMPLE = [18770 17990 20334 15 16727 17750 28006 8308 18 0 1]
Though it is simple to convert for small numbers, I have to convert a very big array. I am using embedded platform so the numbers can not be written to a simple text file for later read in matlab. Its preferable to write the conversion code in matlab.
Edit:
Till now able to get rid of 0x. Failed to get a vector after using eval function as given below:
a='0x4952, 0x4646, 0x4f6e'; %Given
b = strrep(a, '0x', '') ; %Returns 4952, 4646, 4f6e
x = eval( [ '[', b, ']' ] ) %
Copy the code that defines the array to a file called "clip.h".
Write a small c program that #include "clip.h" and use a simple loop to write the data in the desired format. Run the program on your desktop computer. It could look something like this:
#include <stdio.h>
#include <stdint.h>
#include "clip.h"
#define NUMBERS_PER_LINE 20
int main(void) {
int i;
int sample_length = (sizeof AUDIO_SAMPLE)/(sizeof AUDIO_SAMPLE[0]);
printf("AUDIO_SAMPLE = [ ");
for (i=0; i < sample_length; i++) {
printf("%" PRIu16 PRId16, AUDIO_SAMPLE[i]); // Use the format specifier for uint16_t.
if (i % NUMBERS_PER_LINE == NUMBERS_PER_LINE - 1) {
// Insert line continuation mark
printf(" ...\n ");
}
}
return 0;
}
The program will write the matlab code on stdout, so you need to redirect it to the desired file.
It proved tougher than I thought.
Contents of the text file audio.c
4952, 4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1,
0x2, 0xbb80, 0, 0xee00, 0x2, 0x4, 0x10, 0, 0x6166, 0x7463, 0x4,
0, 0xd3cf, 0x3, 0x6164, 0x6174, 0x4f3c, 0xf, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
The given file has 11 values in each line. Here follows the MATLAB code to convert HEX values to decimal numbers:
fp = fopen('audio.c', 'rt');
C = textscan(fp, '%s', 'CommentStyle', '#', 'Delimiter', '');
fclose(fp);
C = regexp(C{:}, '\w+', 'match');
C = cellfun(#(x)strrep(x,'0x',''), C, 'UniformOutput', false);
C = cellfun(#(x)hex2dec(x), C, 'UniformOutput', false);
result=cell2mat(C)'
allOneString = sprintf('%d, ' , result)
Finally I have string of decimal values separated by comma as follows:
18770, 17990, 20334, 15, 16727, 17750, 28006, 8308, 18, 0, 1, 2, 48000, 0, 60928, 2, 4, 16, 0, 24934, 29795, 4, 0, 54223, 3, 24932, 24948, 20284, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Of course previous posts on SO were of immense help :)
I want do to a loop with some variables on my arduino , I have 24 variables ( ConfigSonde[0][3] to ConfigSonde[24][3] ) to change, i need do to a loop:
EX :
ConfigSonde_0[] = {'xxx', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
ConfigSonde_1[] = {'xxx', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
for (i = 0; i < 25; i = i + 1) { ConfigSonde_[i][3]=0;}
Is it possible ?
Thank
You are mixing ints and 'xxx'? (if it's supposed to be a string it must be "xxx"), and this is not legal in C.
ConfigSonde_[i][3] where i = 0 doesn't give you a reference to the variable ConfigSonde_0[3]
Maybe you are looking for:
int ConfigSonde_0[] = {'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
int ConfigSonde_1[] = {'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
int *ConfigSonde [] = {ConfigSonde_0, ConfigSonde_1};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
or
int ConfigSonde[][10] = {
{'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0},
{'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0}
};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
Rewrite: Do do what you need, you're going to have to either add additional variables that point at the things you want to change in the loop, or change your 25 distinct arrays to be a single 2-dimensional array.
You cannot dynamically "compute" names of variables to access their values in C (or C++), since these languages are compiled the names of variables are no longer relevant when the program runs.
I am writing a C program to generate Keys and test them in an encryption function. However since I have NEVER written a C program before, and I'm completely unused to having manage memory manually, I have run into a problem. I have a memory leak and to be honest, I don't know how to solve it. I understand I need to release the memory at some point but can't until I've run through all keys and I run out of memory before I get through all of them. Writing the program in a different language is NOT an option so please do not suggest that. The code that is leaking is shown below, any help would be appreciated.
EDIT: I know i haven't called a free function to free the memory. I don't see where I can put it because I need the memory until i run through all keys. Putting it outside the loops doesn't solve the problem because the leak occurs inside the loops
2nd EDIT: Posted the full program. I do not have the option of using data structures (i.e. the bool arrays) other than those shown because of how the DES encrypt function(which I did not write) works
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
void dec2bin(bool *testaRR, bool *to_return, int convert);
int main(int argc, const char * argv[])
{
// insert code here...
bool testKey[56] = {
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1
};
bool testKey2[56] = {//intuitive key reversed for testing
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0
};
bool output[64];
bool input[64] = {//the reverse of below... DES bits are numbered left to right, in order of least to most significant so we must enter the bit values in reverse.
//forexample the binary vale of N is 01001110 but below is displayed as 01110010
1, 0, 0, 0, 1, 1, 0, 0,//1
0, 0, 0, 0, 1, 1, 0, 0,//0
1, 1, 0, 0, 0, 0, 1, 0,//C
1, 0, 1, 0, 0, 0, 1, 0,//E
1, 1, 0, 0, 1, 0, 1, 0,//S
0, 0, 1, 0, 1, 0, 1, 0,//T
1, 0, 1, 0, 0, 0, 1, 0,//E
0, 1, 1, 1, 0, 0, 1, 0 //N
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int i = y-4; i >= 0; i-=4)
printf("%X", input[i]+2*input[i+1]+4*input[i+2]+8*input[i+3]);//this is the conversion to hex code
printf(")\n");
/*
use char[] to store the key as set of
*/
/*bool input[64] = {//this is the given plaintext message in the intuitive order (opposite of what it is)
0, 1, 0, 0, 1, 1, 1, 0,//N
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 1, 0, 1, 0, 0,//T
0, 1, 0, 1, 0, 0, 1, 1,//S
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 0, 0, 0, 1, 1,//C
0, 0, 1, 1, 0, 0, 0, 0,//0
0, 0, 1, 1, 0, 0, 0, 1 //1
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int j = 0; j < y; j+=4)
printf("%X", input[j+3]+2*input[j+2]+4*input[j+1]+8*input[j]);//this is the conversion to hex code
printf(")\n");*/
bool test [8];
bool returned[8];
char keyphrase [8];
keyphrase[7] = 0;
for(int start = 65; start<=90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for(int two = 65; two<=90; two++){
keyphrase[1]=two;
for(int three = 65; three<=90; three++){
keyphrase[2]=three;
for(int four = 65; four<=90; four++){
keyphrase[3]=four;
for(int five = 65;five<=90;five++){
keyphrase[4]=five;
for( int six = 65; six <=90; six++){
keyphrase[5]=six;
for(int seven = 65; seven <=90; seven++){
keyphrase[6]=seven;
printf("%s \n", keyphrase);
}
}}
}
}
}
//once i fix the memory leak I will be calling the EncryptDes Function here and checking the outputblk agains the given cipher text
}
free(keyphrase);
int k = sizeof(testKey);
printf("(Test Key: ");
for (int z = 0; z < k; z+=7)
printf("%d", testKey[z+7]+2*testKey[z+6]+4*testKey[z+5]+8*testKey[z+4]+16*testKey[z+3]+32*testKey[z+2]+64*testKey[z+1]+ 128*testKey[z]);//this is the conversion to hex code
printf(")\n");
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
printf("(Output MSG: ");
for (int i = 0; i < x; i+=4)
printf("%X", output[i+3]+2*output[i+2]+4*output[i+1]+8*output[i]);//this is the conversion to hex code
printf(")\n");
return 0;
}
void dec2bin (bool *testaRR, bool *to_return, int convert)
{
printf("%d : ", convert);
printf("%c", convert);
printf("\n ");
//bool testaRR [8];
for(int st = 0; st<8; st++){
testaRR[st] = convert%2;
to_return[7-st] = testaRR[st];
//printf("%d :", 7-st);
//printf(" %d spot ", st);
convert = convert/2;
//testaRR stores the arrays in one direction
//to_return stores them in the other
//Example:
//65 = 01000001 testaRR least significant on the far right (m0st sig is in index 7)better for storage and keeping track of where the bits actually are in binary
//65 = 10000010 to_return least significant on the far left (same as DES) (most significant bit is index 0) good for printing to screen
}
You do not need dynamic memory management here.
Start with
char keyphrase[8];
keyphrase[7]=0;
instead of your malloc and you will be good to go. Your highest array index is 7 (the terminating NUL), and hence you need an array of 8 items (0..7).
If you really want to use malloc, simply a free() at the end will be fine, but you need to malloc 8 characters and set keyphrase[7] to 0 to do the terminating NUL still.
Here's a tested version that works:
#include <stdio.h>
/* compile with gcc -Wall -std=c99 keyphrase.c -o keyphrase */
int
main (int argc, char **argv)
{
char keyphrase[8];
keyphrase[7] = 0;
for (int start = 65; start <= 90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for (int two = 65; two <= 90; two++)
{
keyphrase[1] = two;
for (int three = 65; three <= 90; three++)
{
keyphrase[2] = three;
for (int four = 65; four <= 90; four++)
{
keyphrase[3] = four;
for (int five = 65; five <= 90; five++)
{
keyphrase[4] = five;
for (int six = 65; six <= 90; six++)
{
keyphrase[5] = six;
for (int seven = 65; seven <= 90; seven++)
{
keyphrase[6] = seven;
printf ("%s \n", keyphrase);
}
}
}
}
}
}
}
}
The real problem is the use of printf. You did not NULL terminate keyphrase, so every time you printf you overflow.
Also, to avoid the memory leak, simply replace char *keyphrase = (char *)malloc(7); with char keyphrase[8];.
You're calling malloc on the first line but I don't see a single free to release what you allocated. After all the loops are completed (i.e. usage of the allocated data is complete) you must call free(keyphrase);
New answer as the program has been modified.
You say your program is using all memory 'because I'm watching the available memory go down from over 4 free GB to about 5 MB'.
I'm guessing the answer is not the loops but these lines:
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
We can't see the source to or declaration of EncryptDES, but you aren't passing a length to it. If 0 is meant to be the length, this would explain it.
The next line however suggests that output is meant to be an array of 64 bytes (rather than 2 strings). But EncryptDES would have no way of knowing this.
I suggest you run the whole thing under valgrind to find out what is happening.
I am implementing uniform LBP in C. But I am confused regarding the concept. I have implemented LBP. Suppose i have an image of 512*512 dimensions. After LBP it will be 510*510. Now how to get 256 bins/pixels from this LBP image.
for(i=1; i < image_src->width - 1; i++)
{
for(j=1; j < image_src->height - 1; j++)
{
const unsigned char center = image_get_pixel_value(image_src, i, j , 0);
unsigned char code = 0;
if(center <= image_get_pixel_value(image_src, i-1, j-1 , 0))
code += 128;
if(center <= image_get_pixel_value(image_src, i-1, j , 0))
code += 64;
if(center <= image_get_pixel_value(image_src, i-1, j+1 , 0))
code += 32;
if(center <= image_get_pixel_value(image_src, i, j+1 , 0))
code += 16;
if(center <= image_get_pixel_value(image_src, i+1, j+1 , 0))
code += 8;
if(center <= image_get_pixel_value(image_src, i+1, j , 0))
code += 4;
if(center <= image_get_pixel_value(image_src, i+1, j-1 , 0))
code += 2;
if(center <= image_get_pixel_value(image_src, i, j-1 , 0))
code += 1;
image_set_pixel_value(image_tar, i-1, j-1, 0, code);
}
}
And this is the lookup table:
int UniformPattern59[16][16] = {
1, 2, 3, 4, 5, 0, 6, 7, 8, 0, 0, 0, 9, 0, 10, 11,
12, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 14, 0, 15, 16,
17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 20, 0, 21, 22,
23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 27, 0, 28, 29,
30, 31, 0, 32, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 34,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36,
37, 38, 0, 39, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 41,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42,
43, 44, 0, 45, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 47,
48, 49, 0, 50, 0, 0, 0, 51, 52, 53, 0, 54, 55, 56, 57, 58
};
I guess you may misunderstand the concepts about LBP. The LBP has couple of variants: basic LBP, uniform LBP and rotation-invariant uniform LBP.
In the basic LBP, we compare the gray-scale value between the centre pixel and one of its neighbour pixels (interpolated pixels for more accurate result) to get the binary coding (0 or 1) for the bit. Usually, we set the radius of this coding scheme as 1 and the number of neighbours as 8 as default configuration. Therefore, we can get the local binary pattern (LBP) for the centre pixel after comparing all the 8 neighbours, which is a 8-bit binary number, e.g., 01011110 or 11110000. Here, we can see that the range of this kind of LBP is from 0 to 255 if we convert the binary number to decimal number. We usually classify each pixel's LBP code into one of the 256 kinds of patterns to form the histogram for further classification or recognition tasks, which are the 256bins/pixels in your question.
However, there is something different in uniform and rotation-invariant uniform LBPs. That is, the number of patterns is not 256, where the rotation-invariant uniform LBP only has 10 kinds of patterns. The rotation-uniform LBP has 9 rotation-uniform LBPs and 1 others, in which the 9 rotation-uniform LBPs cover 90% patterns over an image usually.
So, in rotation-invariant uniform LBP, you only need to produce a 10-bins histogram for further processing, e.g., classification or recognition. First, you code each pixels as the rotation-invariant uniform LBP, which may produce an image.rows*image.cols matrix. Then, classifying each pattern (the matrix element) to one of the 10 patterns to form an array to represent the histogram.
Some papers for your information:
LBP on Scholarpedia, http://www.scholarpedia.org/article/Local_Binary_Patterns
C++ code by LBP authors, http://www.cse.oulu.fi/CMV/Downloads/LBPSoftware
Multiresolution gray-scale and rotation invariant texture classification with local binary patterns, http://www.rafbis.it/biplab15/images/stories/docenti/Danielriccio/Articoliriferimento/LBP.pdf