Why is my custom block going twice into general_work() function in GNU Radio? - gnuradio

I am creating a custom block "Combine" that gets 20 bytes of data from the first input. The value of first input specifies the number of bytes to be read from the second input, which are read and wrote to the output file.
Whenever I execute the flowgraph, the printing shows that the code goes twice into the general work function. It reads the correct data in the first time and the second time, it just reads bogus values and writes this incorrect data to the output sink.
I am using the following signatures for the input:
Combine_impl::Combine_impl()
: gr::block("Combine",
gr::io_signature::make(2, 2, sizeof(unsigned char)),
gr::io_signature::make(1, 1, sizeof(unsigned char)))
{}
I think my problem is with the forecast function and the usage of consume each function. I have tried doing this in forecast but it still goes twice into the general_work function and writes incorrect data to the output file.
ninput_items_required[0] = 20;
ninput_items_required[1] = 7; //because the first input has a value of 7 and will read 7 bytes of data from the second input
Can someone please help me this to determine what exactly is going wrong over here? Also, how is the consume_each() function supposed to be utilized over here?

I modified the forecast function to take the exact number of items that were utilized from each input:
void Combine_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
ninput_items_required[0] = 20;
ninput_items_required[1] = 7;
}
Instead of using consume_each() which specifies the number of bytes consumed in all of the inputs, I used the consume() function which specifies this number separately for each input:
consume(0, 20); //20 bytes of input at index 0 were consumed
consume(1, 7); //7 bytes of input at index 1 were consumed
And instead of returning noutput_items from the general_work function, I return the following. It exactly the specifies the number of bytes that are to returned, whose value is different than noutput_items.
return (20 + 7);

Related

O(c) Time complexity to read a few gigs text file

I've been tasked to solve a puzzle as follows:
Program an input parser (in C, Python, Java, or Go) that reads a file from standard input and
reads the data into a byte array (8-bit bytes).
checks if the line has a unique set of byte values. If so, it should keep track of the line number Finally, it should print out the line numbers that have a unique set of byte values on each line.
-The program should run in an efficient manner and time – it should not reach big O(n^2)
complexity or worse. Try to see if you can do it in big O(n) time.
- The file should be read into a byte array (8-bit values) without exceeding memory.
I'm reading from the sample file I'm using which is 50MBfile ,
line by line and storing the line in a byte array
then calling a method checkDuplicate(byte[] arr) and pass the byte array
and then create a hashset and loop through the elements of the array and add them to the hashset and then return the size of the hashset.
Since hashsets don't allow duplicates back in the main i check if the returned size is equal to the array size to determine if it is unique or not to save the line number.
private int checkDuplicate(byte[] arr) {
HashSet<Byte> byteSet = new HashSet<Byte>();
int size=0;
for (byte e : arr){
if (e != 0 && byteSet.add(e)) {}
size = byteSet.size();
}
return size;
}
Can O(c) or O(n) be achieved?
I'm getting O(n^2) so far and will handle memory exceptions later on when I reach O(n).
also, would solving the problem in python reduce the time/space complexity?

I am unable to understand the following code can anyone please clear up this to me?

Particularly the usage of keccak256 and the return statement used here.
function isTokenTransferOK(address currentOwner, address newOwner)
public
pure
returns (bool ok)
{
// Check an arbitrary condition to see if transfer should proceed
return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f;
}
abi.encodePacked() essentially concatenates data. So the call here forms a sequence of 40 bytes, where the first 20 bytes are the address currentOwner and the second 20 bytes are the address newOwner.
keccak256() is a hashing function very similar to SHA3. It's used to calculate a hash of the output of abi.encodePacked().
[0] retrieves the first element of an array. In this case, it's the first byte of the hash calculated above.
== 0x7f is true if and only if that byte is the hexadecimal value 0x7f.
So the function hashes the current and new owner addresses and returns true if the first byte of that hash is 0x7f. Otherwise it returns false.

Saving a variable and overwriting it - difficulties

I have a problem. I would try to intialize max to 0, but the thing is that the integer could be negative.
I'm trying to make a program of the form (not exact):
public ... main() {
max=0
x=5
while(x>=0){
(prompt user for int)
(save it)
sum = x + sum;
if (x>max)
max=x;
x++;
(print max and sum)
}
}
This is the question:
Write a program that reads 5 integers from a file, computes their sum and maximum and prints these values to the screen. Do this by modifying the summing program from the chapter. Insert a new int variable called max which you should initialize to the first value in the file. This will call for an extra set of input statements before the loop starts. To compute the maximum you will need an if statement nested inside the loop.
Thanks alot everyone!
You answer the problem yourself in the question :)
Don't give max any hard-coded initial value. Always set it to the first integer in the file, and then compare every time after that.

Converting 16Bit PCM Values into -1 to 1 values

This is my first post, so I hope someone can help!
I am reading in audio data (in CoreAudio) using the AudioFileReadPackets function, this code is working correctly and loads the 16 bit PCM values into a buffer.
The first sample value is this: '65491' (There is silence at the beginning of this audio). I understand that this is an Unsigned integer, so my question is, how to convert this value to a range of -1 to 1.
Currently I am dividing the sample value by 32768.0 into a float variable, like so...
for (UInt32 i = 0; i < packetCount; i++){
sample = *(audioData + i);
//turn it into the range -1.0 - 1.0
monoFloatDataLeft[i] = (float)sample / 32768.0;
}
However, for the sample given above (as example) this results in an output of '1.998626709' which is not zero (as it should be for silence)?
Saying this, when I look at a sample much later on in the file, the value of which i know to be around the '0.3' mark, the result of the algorithm comes out at '0.311584473' which i believe to be correct?
So why are the first samples not being read as zero, as i know them to be?
You need to subtract 32768 from your unsigned data first, so it's 0 centered.

Guess a buffer size and recall an api function or call it everytime twotimes to get exact buffer size

I want to retrieve the version information of msi-package(s)
What's the better way?
First: Guessing a buffer that is large enough and recall if it doesn't fit (ERROR_MORE_DATA)
1 func call vs. 3 func calls and buffer can be bigger then needed
Second: Call the api function to get the buffer size and then recall it to get the string with a (perfect) matching buffer size
2 func calls every time with a perfect buffer size
It's about (1 or 3) function call(s) vs. 2 function calls every time.
Is there any best practice for this "problem"?
I hope to get a generalized answer (assume calling function is really time consuming and/or buffer size can be very different (10 bytes to 200 megabyte) for further code writing. :-)
pseudo code:
First:
StringBuffer = 10 // (byte) guessing returned string will fit in 10 bytes
result = MsiGetProductInfoW(
product,
INSTALLPROPERTY_VERSIONSTRING,
VersionString,
StringBuffer
); //maybe it fits in 10
if result = ERROR_MORE_DATA then //doesnt fit in 10 so recall to get the correct buffer size
begin
MsiGetProductInfoW(
product,
INSTALLPROPERTY_VERSIONSTRING,
nil,
StringBuffer
);
Inc(StringBuffer); // cause null-terminated string
// recall it with matching
MsiGetProductInfoW(
product,
INSTALLPROPERTY_VERSIONSTRING,
VersionString,
StringBuffer
);
end;
Second:
StringBuffer = 0;
// get buffer size
MsiGetProductInfoW(
product,
INSTALLPROPERTY_VERSIONSTRING,
nil,
StringBuffer
);
Inc(StringBuffer); // cause null-terminated string
// use it with the correct buffersize
MsiGetProductInfoW(
product,
INSTALLPROPERTY_VERSIONSTRING,
VersionString,
StringBuffer
);
Thank you!
In your First option, you can skip the second call, because even on the failing first call, the needed size should be stored in StringBuffer.
This makes the choice (1 or 2) vs. (always 2). That should be clear enough. Further, it shouldn't be hard to come up with a reasonable-sized buffer, that will pass 90+% of the time.