Dependency between wave data format and input buffer size - wave

I do some quick solution using VFW.
At the stage of preparing to receive audio data I ask VFW to give me the default wave data format for the attached WEBcam.
It returns me WAVEFORMATEX struct, pls, see the pic.
As you can see it's 1 byte for sample and 1 channel.. 11025 bytes per sec.
But the callBack receives another amount o bytes, pls, see the pic
And the question is: what is the correlation and dependency between these to values?
How to separate data in such case if there are 2 channels?

dwBufferLength is the size of the buffer, not the amount of captured bytes. You are interested in another member: dwBytesRecorded.
In case of stereo WAVEFORMATEX will have 2 in nChannels, and then nBlockAlign, nAvgBytesPerSec will be adjusted respectively. The bytes in the buffers will have the following packing:
0000: [sample 0, channel 0]
0001: [sample 0, channel 1]
0002: [sample 1, channel 0]
0003: [sample 1, channel 1]
...

Related

How to decompress CCITT Group 4 Algoritms?

I'm trying to code decompression CCITT Group4 algorithm using my matlab code editor.
But, I cannot understand decoding mechanism.
when we finish encoding an original binary image, we wil have only encoded binary code.
so, In that code, there are no a0, a1, a2, b1, b2(you know, CCITT G4 elements).
This point is My question. Let me give you an example.
we have compressed code line(0 0 0 1 0 0 1 0 1 1 1 1 1).
Firstly, you can detect "Pass mode codeword(0 0 0 1)",
and "Horizontal mode codeword(0 0 1 0 1 1 1 1 1)".
we can know the decoding code run length about horizontal mode, ("White run length=2 -> 0 1 1 1", "Black run length=2 -> 1 1")
But, How can we know the decoding code run length about pass mode??
I think there is no information about pass mode cord run length. Please let me know. Thank you.
CCITT G4 is a 2-Dimensional image encoding algorithm. The pass code and other codes (e.g. the vertical codes) use the previous line as a reference. After a line is decoded, it becomes the "previous" line and a new "current" line begins.

Number of parameters calculation in Convolutional NN

I'm new in the CNN study and I started by watching Andrew'NG lessons.
There is an example that I did not understand :
How did he compute the #parameters value ?
As you can see in Answer 1 of this StackOverflow question, the formula for the calculation of the number of parameters of a convolutional network is: channels_in * kernel_width * kernel_height * channels_out + channels_out.
But this formula doesn't agree with your data. And in fact the drawing you are showing does not agree with the table you are giving.
If I base myself on the drawing, then the first CN has 3 entry channels, a 5*5 sliding window and 6 output channels, so the number of parameters should be 456.
You give the number 208, and this is the number obtained for 1 entry channel and 8 output channels (the table says 8, while the drawing says 6). So it seems that 208 is correctly obtained from the table data, if we consider that there is one input channel and not three.
As for the second CN, with 6 entry channels, a sliding window 5*5 and 16 output channels, you need 2,416 parameters, which looks suspiciously close to 416, the number given in the table.
As for the remaining networks it is always the number of input dimension times the number of output dimensions, plus one: 5*5*16*120+1=48,001, 120*84+1=10,081, 84*10+1=841.

Matlab fwrite saturation

I have an image (matrix) that has values on 16 bits, that is between 0 and 65535 and I want to write it in a binary file, so I am using fwrite, as it says in the documentation I have tried to use different precision to write the data on 2 bytes ('integer*2', 'uint16', etc), but it seems that the data gets saturated on 15 bits, that is the maximum value is 0x7ff, if I use more bytes, let's say 4, the data arrives complete, with values greater than 0x7ff and less than 0xffff. I read in the documentation that fwrite saturate the values so there will be no Inf or NaN, does that mean that I can write on x bytes, just (x*8 - 1) bits ?!?
Is there any other way to write the image to a bin file with the correct values on 2 bytes ?
Can you run this code and verify it works on your system?
%generate and show data
IM = uint16(((2^16)-1) .* rand(512));
imagesc(IM);axis image;colorbar
%write data
fid=fopen('image.dat','w');
fwrite(fid,IM(:),'uint16');
fclose(fid);
%read data
fid=fopen('image.dat','r');
IM2=fread(fid,inf,'*uint16');
fclose(fid);
IM2=reshape(IM2,512,512);
%check if they are equal
all(IM(:)==IM2(:))
>> 1
If this works, can you check where it differs from your code?

How to encode packet information by XOR in matlab

I am working in channel coding part. My major is encoding with bit information. For example, given a input bit is x=[1 0 1] and G matrix is
G =
1 0 0 1
0 0 1 0
1 1 0 1
then encoding symbol will be y=mod(x*G,2)% mod to convert to binary bit
y =
0 1 0 0
It is very simple idea for bits encoding processing in channel coding. Now I want to do map this work to packet encoding. Some paper mentioned that we can do this way for packet encoding if we consider each packet is n bytes. For example, I have a text file that size is 3000bytes. Now I will divide the file into 1000 packet. Hence, each packet has size is that 3 bytes. My problem is that I done with bit encoding. However, I don't have any idea to work with packet encoding. Please let me know if you worked with this problem. I hear that bit encoding we only has two level 0 and 1, so we can use GF=2. If we work for packet level, we must consider GF>2. I am not sure it
Second question is that how about if packet size equals 50 bytes instead of 3 bytes?
First, when you talk about GF(p^m), you mean Galois Field, where p is a prime number that indicates the degree of the polynomial. Hence, it is possible to define a field with pm elements. In your case, p=2 and m=3. Once, you have the polynomial, you can apply it using two different encoding schemes:
Block coding. You can split the packet into several blocks of 3 bits and encode each block independently. Use zero-padding in case they are not multiples.
Convolutional coding. You apply the polynomial as a sliding algorithm.
The second approach is more robust and has lower delays.
The GF terminology is used to work with polynomials, the concepts of extended Galois Fields and polynomials are not very complicated, but it takes time to understand it. However, in practice, if you have a polynomial expression you just apply it to the bit sequence in the fashion that you want, block or convolutional (or fountain).
You matrix G is actually defining 4 polynomials for the input X=[1 x x^2], namely Y1=1+x^2, Y2=x^2 and Y3=x, Y4=1+x^2

Compression in Scala

I'm working on Scala with VERY larg lists of Int (maybe large) and I need to compress them and to hold it in memory.
The only requirement is that I can pull (and decompress) the first number on the list to work with, whithout touching the rest of the list.
I have many good ideas but most of them translate the numbers to bits.
Example:
you can write any number x as the tuple |log(x)|,x-|log(x)| the first element we right it as a string of 1's and a 0 at the end (Unary Code) and the second in binary. e.g:
1 -> 0,1 -> 0 1
...
5 -> 2,1 -> 110 01
...
8 -> 3,0 -> 1110 000
9 -> 3,1 -> 1110 001
...
While a Int takes a fixed 32 bits of memory and a long 64, with this compression x requires 2log(x) bits for storage and can grow indefinetly. This Compression does reducememory in most cases.
How would you handle such type of data? Is there something such as bitarray or something?
Any other way to compress such data in Scala?
Thanks
Depending on the sparseness and range of your data set, you may keep your data as a list of deltas instead of numbers. That's used for sound compression, for instance, and can be both lossy or lossless, depending on your needs.
For instance, if you have Int numbers but know they will hardly ever be more than a (signed) Byte apart, you could do something like this list of bytes:
-1 // Use -1 to imply the next number cannot be computed as a byte delta
0, 0, 4, 0 // 1024 encoded as bytes
1 // 1025 as a delta
-5 // 1020 as a delta
-1 // Next number can't be computed as a byte delta
0, 0, -1, -1 // 65535 encoded as bytes -- -1 doesn't have special meaning here
10 // 65545 as a delta
So you don't have to handle bits using this particular encoding. But, really, you won't get good answers without a very clear indication of the particular problem, the characteristics of the data, etc.
Rereading your question, it seems you are not discarding compression techniques that turn data into bits. If not, then I suggest Huffman -- predictive if needed -- or something from the Lempel-Ziv family.
And, no, Scala has no library to handle binary data, unfortunately. Though paulp probably has something like that in the compiler itself.