How to do dequentize with the output of quantized tflite? - quantization

I have convert a PB file to tflite with uint8 quantization. I would like to know how to convert the output from uint8 back to float so that the previous code can still work.

Usually, the range of float is [-1..1], so the formula is
putFloat((uint8Val - 127.5)/127.5)
But this actually depends on the quantization parameters mean and std you used. Note that recently tflite switched to int8 in range [-128..127].

Related

FFmpeg AVFrame Audio Data Modification

I'm trying to figure out how FFmpeg saves data in an AVFrame after the audio has been decoded.
Basically, if I print the data in the AVFrame->data[] array I get a number of unsigned 8 bit integers that is the audio in raw format.
From what I can understand from the FFmpeg doxygen, the format of the data is expressed in the enum AVSampleFormat and there are 2 main categories: interleaved and planar. In the interleaved type, the data is all kept in the first row of the AVFrame->data array with size AVFrame->linesize[0] while in the planar type each channel of the audio file is kept in a separate row of the AVFrame->data array and the arrays have as size AVFrame->linesize[0].
Is there a guide/tutorial that explains what do the numbers in the array mean for each of the formats?
Values in each of the data arrays (planes) are actual audio samples according to specified format. E.g. if format is AV_SAMPLE_FMT_S16P it means that data arrays actually are arrays of int16_t PCM data. If we have deal with mono signal - only data[0] is valid, if it is stereo - data[0] and data[1] are valid, so on.
I'm not sure that there is any guide that can help you to explain each particular case but anyway described approach is quite simple and is easy to understand. You should just play a bit with it and thing should become clear.

Send converted DS1820 temperature over PIC16 uart

I'm trying to send a converted temperature reading from my DS1820 to my PC using a PIC16F877 uart. I am using MPLABX and the XC8 compiler, which has a build in usart.h, though it's only useful for the PIC18 series, so I'm using usart_pic16.h which has been written to work with the PIC16 series by a third party.
I am successfully collecting the temperature in its hex form from the DS1820 and have converted it to a human readable float, but I can't find a way to forward the float value to the PC via the UART.
The usart_pic16.h library allows for direct sending of chars, strings, lines and ints over the usart using the following methods:-
void USARTWriteChar(char ch);
void USARTWriteString(const char *str);
void USARTWriteLine(const char *str);
void USARTWriteInt(int16_t val, int8_t field_length);
I'm stuck at finding a way to send the float value across the uart using this library, which includes extraction and sending a decimal point.
I did try sending a string like this:-
USARTWriteString( "TempC= %7.3f degrees C \r\n", temp_c );
Where temp_c is the float value of the temp, but it errorred with "too many function arguments" while compiling. Probably obvious to those c gurus out there, which I'm unfortunately not :(
Maybe one way would be to extract each value from the float and sent it as an int, with the exception of the decimal point which could probably be found with an 'if' check of each value, then when the decimal point is found just send it as a char e.g. USARTWriteChar('.');, which does work. Unfortunately I don't know how to extract individual float values or if it's the best way to do it.
I wasn't sure if my code was required to solve this so thought I'd avoid spamming it unless someone asks.
Any help would be great.
Thanks.
The general equivalent would be to include <stdio.h> and do something like the following:
char s[64];
sprintf(s, "TempC= %7.3f degrees C \r\n", temp_c);
USARTWriteString(s);
Although for an embedded platform you may be best to avoid the printf style functions that can use a fair bit of code space on a small microcontroller. Also in the above example it would make sense to break just the floating point conversion into a seperate sprintf and output the rest of the string seperately so the buffer s doesn't have to be so large.
That should get you running for the moment but in the longer term you might want to look at converting the temperature to integer say by multiplying it by 1000 and then decoding the value on the PC, that's assuming eventually you intend to write your own application to communicate with the microcontroller.

Force dlmread to return uint8 matrix - possible?

I have a file containing a very very huge matrix, size in millions x hundreds, and I wanna further process this matrix on and at the same time, conserve memory. But unfortunately, dlmread returns a double type matrix.
The numbers on this file are 0-255 only, so uint8 is the most suitable. But I have hit my memory limit, and Matlab starts yelling out "Out of memory" error, when I tried to convert the loaded matrix into uint8, with myMat = single(myMat); It makes sense, because a new matrix has to be created before removing the old one.
Can I do anything with this?
You could convert your data file to a suitable (i.e. lossless) 8 bit image format (using an external program) and then read it into MATLAB with imread. Reading this file should be a lot quicker too, as there is no data conversion involved.

cvSobel with 16 bit grayscale images

I have a 10 bit grayscale image that I like to use the cvSobel function. The documentation is not clear if it can accept 16 bit grayscale images. When I try to make this work, I get an exception from OpenCV.
In my VC++ when I look at the function, this is what I am seeing.
CVAPI(void) cvSobel( const CvArr* src, CvArr* dst,
int xorder, int yorder,
int aperture_size CV_DEFAULT(3));
I tried to make destination 32F but still crash.
It says here:
To avoid overflow, the function requires 16-bit destination image if
the source image is 8-bit. The result can be converted back to 8-bit
using cvConvertScale or cvConvertScaleAbs functions. Besides 8-bit
images the function can process 32-bit floating-point images. Both
source and destination must be single-channel images of equal size or
ROI size.
So, you could probably convert your source to 8bit and then declare the destination as 16bit.
It would also help if you could post the specific part of the code if this doesn't work.

comparing these two audio files

i have a two audio files one is original file and another i have corrupted it by reversing some bits, how to compare the quality of these two files is there any algorithm or an software where i can compare the quality of the two files.
"any algorithm or an software": Do you want to program or not?
If you want a software to do this for you: stackoverflow cannot help you
If you are willing to program (at least call functions in a library) that's a different story:
There are some libraries which can do this, specifically to convert the audio from compressed to WAVEFORM format in the first place (the library-to-choose depends on which format your audio is in). Or is your audio in waveform format already? you didnt tell. If you have the audio in waveform format (raw audio in e.g. * signed 16bit mono at 22khz) you can easily program this yourself: Since the only damage you did to your audio is bitflips you can iterate throught them and just sum the differences up: you have to take in account the format the waveform is in tho: you cannot compare the bit-level (because each bit has different significance); if you have * signed 16bit audio you have to use in C the type int so that A) the comparison is signed and B) the difference does not overflow.
One physical measurement for the quality of sound is the SNR: http://en.wikipedia.org/wiki/Signal-to-noise_ratio. I don't know of any lib that does that for you, but it is not to hard to do yourself:
calculate the noise: noise[n] = manipulated[n] - original[n], n = sample index
calculate the power of "noise" and of "original": p_noise[n] = noise[n] * noise[n], ...
get the SNR by dividing the values = SNR[n] = p_original[n]/p_noise[n]
You may want to calculate an average(!) SNR... I hope you can figure out how to do that yourself. This should put your on the right track.

Resources