Processing Raw data from kinect in matlab - matlab

I am using Microsoft kinect v1 in MATLAB and want to get depth data on every pixel in meters.
I am not sure how to get that data because I am getting uint16 and as far as I have read is that it provides depth only in 13 bits, So how do I get those 13 bits and do some conversion to get depth exactly in meters.
I have searched a lot about it but could not get to any conclusion.
Kinectinfo = imaqhwinfo('kinect');
colorinfo = Kinectinfo.DeviceInfo(1);
depthinfo = Kinectinfo.DeviceInfo(2);
colorvid = videoinput('kinect',1);
depthvid = videoinput('kinect',2);
srcDepth = getselectedsource(depthvid);
% Set the frames per trigger for both devices to 1.
colorvid.FramesPerTrigger = 1;
depthvid.FramesPerTrigger = 1;
% Set the trigger repeat for both devices to 200, in order to acquire 201 frames from both the color sensor and the depth sensor.
colorvid.TriggerRepeat = 200;
depthvid.TriggerRepeat = 200;
%Configure the camera for manual triggering for both sensors.
triggerconfig([colorvid depthvid],'manual');
% Start both video objects.
start([colorvid depthvid]);
%Trigger the devices, then get the acquired data.
% Trigger 200 times to get the frames.
for i = 1:200
% Trigger both objects.
trigger([colorvid depthvid])
% Get the acquired frames and metadata.
[imgColor, ts_color, metaData_Color] = getdata(colorvid);
[imgDepth, ts_depth, metaData_Depth] = getdata(depthvid);
end
[NYU Depth and RGB image][1]
[Histogram of swaped Raw Depth image][2]
[Histogram of Raw Depth Image][3]
I would like to have some code for conversion or any SDK that provides me with meters in matlab.
Thanks alot.

According to several articles, eg. such as here and here, the first three bits are code for the player that the device has identified, but this is only activated if skeleton tracking is enabled. If you don't enable skeleton tracking, the three bits are set to zero.
If you are using matlab, the depth images are 16bit double images, and contain data that is already extracted from the 13bits (most probably because skeleton tracking is already disabled, hence 3 MSB are zero.) Therefore you dont need to convert/extract the 13 bit data in matlab or libfreenect etc becuase the bits be already zero.
According to the Matlab help page, matlab ImAq toolbox requires the installation of Kinect for Windows Runtime; because it uses the Kinect SDK Kinect drivers to acquire data. And according to this SO answer, "using Microsoft SDK, so the values that are returned from kinect sensor are already real distances in mm."
Therefore, you dont really need to bitshift data in matlab. The only reason you would need to do it, would be if you are getting raw depth stream from the kinect drivers (?) in C++/C# and the 16bit data would include the 3 LSB bits.

Related

Unity show audio spectrum(wave) when upload audio file

I am trying to implement a spectrum of audio files that are part of Samsung's voice recording capabilities.
Like this
enter image description here
But, there is information showing the spectrum in real time, but there is no information showing the wave of the uploaded audio file.
What you are likely looking for is to display the overall amplitude of the audio data that you are getting. The data you get from Unity with GetSpectrumData however is an array showing the amplitude at a bunch of different frequencies. To find a suitable "overall" amplitude, you could either average, find the max, or sum up the contents of this array, then use this new value to draw your visualization. I'd probably go with finding the maximum, i.e. the highest/loudest value in the array. You could do this by using Linq:
using System.Linq;
/*...*/
float loudest = yourSpectrumDataArray.Max();
Note also that the amplitude values you get from GetSpectrumData are tiiiiny, and you might want to convert them to decibel values before working with them. I'm using this equation - can't remember where I found it but it works for me:
public float AmplitudeToDB(float value)
{
return 20.0f * Mathf.Log10(value / 2.5f + 1.5849e-13f);
}

Playing sound in Matlab at +30dB

As far as I know when I load wav files to matlab with command:
song = wavread('file.wav');
array song have elements with values from -1 to 1. This file (and hardware) is prepared to be played with 80dB. I need to add +30dB to achieve 110dB.
I do +10dB by multiplying by sqrt(10), so to get +30dB I do:
song = song*10*sqrt(10); which is the same as
song = song*sqrt(10)*sqrt(10)*sqrt(10);
Now values of array song have much greater values than -1 to 1 and I hear distorted sound.
Is it because of this values greater than <-1,1> or quality of my speakers/headphones?
The distortion is because your values are exceeding +/-1. The float values are converted to ADC counts, which are either +/-32768 (for a 16-bit ADC) or +/-8388608 (for a right-justified 24-bit ADC) or +/-2147483648 (for a left-justfied 24-bit ADC). For a 16-bit ADC, this is usually accomplished by an operation like adcSample = (short int)(32768.0*floatSample); in C. If floatSample is > +1 or < -1 this will cause wraparound in the short int cast, which is the distortion you hear. The cast is necessary because the ADC expects 16-bit digital samples.
You will need to adjust your amplifier/speaker settings to get the sound level you desire.
Conversely, you could create a copy of your file, lower it by 30 dB, adjust your amplifier/speakers to play the new file at 80 dB, then play the original file at the same amp/speaker settings. This will cause the original file to be played at 110 dB.
As Paul R noted in his comment, I am guessing here that you are using dB as shorthand for dB SPL when referring to the actual analog sound level produced by the full signal chain.

Sine LUT VHDL wont simulate below 800 hz

I made a sine LUT for VHDL, using 256 elements.
Im using MIDI input, so values range 8.17Hz (note #0) to 12543.85z (note #127).
I have another LUT that calculates how many value must be sent to my 48 kHz codec in order to play the sound (the 8.17Hz frequency will need 48000/8.17 = 5870 values).
I have another LUT that contains an index factor, which is 256/num_Values, which is used to call values from the sin table (ex: 100*256/5870 = 4 (with integer rounding)).
I send this index factor to another VHDL file, which is used to calculate which value should be sent back. (ex: index = index_factor*step_counter)
When I get this index, I divide it by 100, and call sineLUT[index] to get the value that I need to generate a sine wave at the desired frequency.
The problem is, only the last 51 notes seem to work for me, and I do not know why. It seems to get stuck on a constant note at anything below that frequency (<650 hz) , and just decrease in volume every time I try to lower the note.
If you need parts of my code, let me know.
Just guessing, I suspect your step_counter isn't going through enough cycles, so your index (into the sine lut) doesn't go through a full 360 degrees for the lower frequencies.
For anything more helpful, you'll probably have to post code.
As an aside, why aren't you using something more like a conventional DDS? Analog Devices has a nice write-up on the basics: DDS Tutorial

Aligning two wav files precisely

I have a tool which compares two audio wav files frame by frame and returns a grade which gives the level of similarity between the two files.
I have an original wav file and a recording of the wav file, since the two files are almost similar i should get a high score of similarity, yet i get a poor score, mainly due to a very slight delay in the recorded file-leading to frame mismatch
My question is- how do i go about aligning the two audio files exactly using MATLAB, so that a valid frame to frame comparison may be done.
You should run a series of comparisons, shifting one of the frame in time and calculating the correlation between two. Highest value of correlation will give you time shift between waves.
I think you can use xcorr to achieve this.
Having had the same problem and without success to find a simple tool to sync the start of video/audio recordings automatically,
I decided to make syncstart (github).
It is a python-based command line tool that calculates the cut needed to bring the recordings into sync.
It uses an fft-based correlation of the start.
The basic code should be easily convertible to matlab:
corr = fft.ifft(fft.fft(s1pad)*np.conj(fft.fft(s2pad)))
ca = np.absolute(corr)
xmax = np.argmax(ca)
if xmax > padsize // 2:
offset = (padsize-xmax)/fs
#second signal (s2) to cut
else:
offset = xmax/fs
#first signal (s1) to cut

Sound output levels in MATLAB

Thanks to Yair Altman's SoundVolume.m I can control the system speaker output volume from MATLAB. However, MATLAB's sound output is still at a far lower level than, say, a random YouTube video or the test sound in the Realtek HD audio manager (latest version, 6.0.1.6080).
I'm using Windows 7, whose sound level controls are well known to be less than transparent, but MATLAB's own control in the Windows Volume Mixer is set to max -- yet MATLAB's sound level output is way less than anything else.
Any suggestions? Here is what I'm using to play a sound (a 3kHz pure tone):
Fs = 22100;
x = 0:1/Fs:1;
y = sin(2*pi*3000.*x);
wavplay(y,22100)
Have you tried increasing the amplitude?
volume = 5; % 1 is normal, 0 is mute, >1 is louder
wavplay(y*volume, 22100);