The wav file has a header (44 bytes). In this header is specified the sample rate of the signal, number of channels and so on, number of samples from the audio file.
I need to know where can I find the number of samples information in the header.
What would be the formula.
Starting at the 40th byte for the next 4 bytes (little endian) is the Subchunk2 size. This can also be deduced from the formula:
Subchunk2size = NumSamples * NumChannels * BitsPerSample/8
NumChannels start at byte 22 and 2 bytes (little endian) in length. BitsPerSample start at 34th byte and is 2 bytes (little endian) in length. Replacing all these you can get the NumSamples which is the number of samples.
For example: if Subchunksize2=2048, NumChannels=2 and BitsPerSample=16, you get
2048 = NumSamples * 2 * 2 so NumSamples=512
A good read is here.
Related
https://learn.microsoft.com/en-us/dotnet/api/system.convert.tobase64string?view=net-5.0
It says
If an integral number of 3-byte groups does not exist, the remaining
bytes are effectively padded with zeros to form a complete group. In
this example, the value of the last byte is hexadecimal FF. The first
6 bits are equal to decimal 63, which corresponds to the base-64 digit
"/" at the end of the output, and the next 2 bits are padded with
zeros to yield decimal 48, which corresponds to the base-64 digit,
"w". The last two 6-bit values are padding and correspond to the
valueless padding character, "=".
Now,
Imagine that the byte array I send is
0
So, only one byte, namely 0
That one byte will be padded right into 000 right?
So now, we will have something like 0=== as the encoding because it takes 4 characters in base 64 encoding to encode 3 bytes.
Now, we gonna decode that.
How do we know that the original byte isn't 00, or 000, but just 0?
I must be missing something here.
So now, we will have something like 0=== as the encoding
3 padding characters is illegal. This would mean 6 bit plus padding.
And then 0 as a byte value is A in Base64, so it would be AA==.
So the first A has the first 6 bits of the 0 byte, the second A contributes the 2 remaining 0 bits for your byte, and then there are just 4 0 bits plus the padding left, not enough for a second byte.
How do we know that the original byte isn't 00, or 000, but just 0?
AA== has only 12 bits (6 bits per character) so it can only encode 1 Byte => 0
AAA= has 18 bits, enough for 2 bytes => 00
AAAA has 24 bits = 3 bytes => 000
I am writing a JPEG parser/modifier/unparser. First, to make sure we are using the same terninology I include
a definition adapted from a very useful source
DHT( Define Huffman Table) marker:
Field Size Description
Marker Identifier 2 bytes 0xff, 0xc4 to identify DHT marker
Length 2 bytes This specify length of Huffman table
HT information 1 byte bit 0..3 : number of HT (0..3, otherwise error)
bit 4 : type of HT, 0 = DC table, 1 = AC table
bit 5..7 : not used, must be 0
Number of Symbols 16 bytes Number of symbols with codes of length 1..16,
the sum(n) of these bytes is the total number of
codes, which must be <= 256
Symbols n bytes Table containing the symbols in order of
increasing code length ( n = total number of
codes ).
My Question:
The Symbols must be ordered by increasing code length. But within each code length do they have to be ordered (eg by increasing value).
The reason I ask is that my table generated from frequencies collected from the AC scan data yields the right bit lengths, but they are not in increasing value order within the tree (when read in depth-first traversal order). I need to order these and regenerate the tree to get the same bit pattern as when I write and read my table.
I suspect that this is because in my write routine I specifically order the Symbols by bit length then value. If ordering by value is unnecessary I can remove the overhead (in code and at runtime).
The codes are implicitly ordered by increasing value. The DHT marker only specifies the number of codes of each length. The procedure in the JPEG standard describes how to generate the actual Huffman code for each value.
I would like to count '01' sequence in 5760 binary bits.
First, I would like to combine several binary numbers then count # of '01' occurrences.
For example, I have 64 bits integer. Say, 6291456. Then I convert it into binary. Most significant 4 bits are not used. So I'll get 60 bits binary 000...000011000000000000000000000
Then I need to combine(just put bits together since I only need to count '01') first 60 bits + second 60 bits + ...so 96 of 60 bits are stitched together.
Finally, I want to count how many '01' appears.
s = binToString(5760 binary bits)
cnt = s.count('01');
num = 6291226
binary = format(num, 'b')
print(binary)
print(binary.count('01'))
If I use number given by you i.e 6291456 it's binary representation is 11000000000000000000000 which gives 0 occurrences of '01'.
If you always want your number to be 60 bits in length you can use
binary = format(num,'060b')
It will add leading 0 to make it of given length
Say that nums is your list of 96 numbers, each of which can be stored in 64 bits. Since you want to throw away the most 4 significant bits, you are really taking the number modulo 2**60. Thus, to count the number of 01 in the resulting string, using the idea of #ShrikantShete to use the format function, you can do it all in one line:
''.join(format(n%2**60,'060b') for n in nums).count('01')
I have a buffer that is filled with data and begins with < Buffer 52 49 ...>
Assuming this buffer is defined as buf, if I run buf.readInt16LE(0) the following is returned:
18770
Now, the binary representation of hex values 52 and 49 are:
01010010 01001001
If I were to convert the first 15 bits to decimal, omitting the 16th bit for two's complement I would get the following:
21065
Why didn't my results give me the value of 18770?
18770 is 01001001 01010010 which is your 2 bytes reversed, which is what the readInt*LE functions are going to do.
Use readInt16BE.
You could do this: parseInt("0x" + buf.toString("hex")). Probably a lot slower but would do in a pinch.
In Wiki "dynamic range" is defined as "the ratio of the amplitude of the loudest possible undistorted sine wave to the root mean square (rms) noise amplitude", but I'm not clear about how should I use these operands.
I have read in an uncompressed .wav file. It uses 16 bits per sample, and I've converted these bytes to integers (may range from -32768 to 32767). The largest int is 31692 and the smallest -32764. So what should I do next? I saw the formula "20 * log (high / low)" and it doesn't seem to work directly. Could you please show me the calculation steps? Thanks.
I've solved this problem. Actually the formula "20 * log (high / low)" works. "high" should be abs(-32764) = 32764, and low should be the value most near 0 but not 0, which is 1 in my file. So the dynamic range is 20 * log10(32764 / 1) = 90 dB.