-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR2 - Audio - To decibels HOST Tensor #150
Changes from 2 commits
dcd9833
ed1e425
8e05043
23ba5a5
337ccc1
d1b5b41
c413836
527ed18
7753fda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright (c) 2019 - 2023 Advanced Micro Devices, Inc. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
#include "rppdefs.h" | ||
#include <omp.h> | ||
|
||
RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, | ||
RpptDescPtr srcDescPtr, | ||
Rpp32f *dstPtr, | ||
RpptDescPtr dstDescPtr, | ||
RpptImagePatchPtr srcDims, | ||
Rpp32f cutOffDB, | ||
Rpp32f multiplier, | ||
Rpp32f referenceMagnitude, | ||
rpp::Handle& handle) | ||
{ | ||
Rpp32u numThreads = handle.GetNumThreads(); | ||
|
||
// Calculate the intermediate values needed for DB conversion | ||
Rpp32f minRatio = std::pow(10, cutOffDB / multiplier); | ||
if(minRatio == 0.0f) | ||
minRatio = std::nextafter(0.0f, 1.0f); | ||
|
||
const Rpp32f log10Factor = 0.3010299956639812; //1 / std::log(10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initialize as #define in rpp_cpu_common There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initializing that as #define even at the start of this file lead to output mismatches, thus left it as such There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @snehaa8 Then instead of #define, lets just move the const Rpp32f as-is to rpp_cpu_common |
||
multiplier *= log10Factor; | ||
|
||
omp_set_dynamic(0); | ||
#pragma omp parallel for num_threads(numThreads) | ||
for(int batchCount = 0; batchCount < srcDescPtr->n; batchCount++) | ||
{ | ||
Rpp32f *srcPtrCurrent = srcPtr + batchCount * srcDescPtr->strides.nStride; | ||
Rpp32f *dstPtrCurrent = dstPtr + batchCount * dstDescPtr->strides.nStride; | ||
|
||
Rpp32u height = srcDims[batchCount].height; | ||
Rpp32u width = srcDims[batchCount].width; | ||
Rpp32f refMag = referenceMagnitude; | ||
|
||
// Compute maximum value in the input buffer | ||
if(!referenceMagnitude) | ||
{ | ||
refMag = -std::numeric_limits<Rpp32f>::max(); | ||
Rpp32f *srcPtrTemp = srcPtrCurrent; | ||
if(width == 1) | ||
refMag = std::max(refMag, *(std::max_element(srcPtrTemp, srcPtrTemp + height))); | ||
else | ||
{ | ||
for(int i = 0; i < height; i++) | ||
{ | ||
refMag = std::max(refMag, *(std::max_element(srcPtrTemp, srcPtrTemp + width))); | ||
srcPtrTemp += srcDescPtr->strides.hStride; | ||
} | ||
} | ||
} | ||
|
||
// Avoid division by zero | ||
if(!refMag) | ||
refMag = 1.0f; | ||
|
||
Rpp32f invReferenceMagnitude = 1.f / refMag; | ||
// Interpret as 1D array | ||
if(width == 1) | ||
{ | ||
for(Rpp32s vectorLoopCount = 0; vectorLoopCount < height; vectorLoopCount++) | ||
{ | ||
*dstPtrCurrent = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent) * invReferenceMagnitude)); | ||
srcPtrCurrent++; | ||
dstPtrCurrent++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use post increment ++ at L84, and make loop single line removing braces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} | ||
else | ||
{ | ||
Rpp32f *srcPtrRow, *dstPtrRow; | ||
srcPtrRow = srcPtrCurrent; | ||
dstPtrRow = dstPtrCurrent; | ||
for(int i = 0; i < height; i++) | ||
{ | ||
Rpp32f *srcPtrTemp, *dstPtrTemp; | ||
srcPtrTemp = srcPtrRow; | ||
dstPtrTemp = dstPtrRow; | ||
Rpp32s vectorLoopCount = 0; | ||
for(; vectorLoopCount < width; vectorLoopCount++) | ||
{ | ||
*dstPtrTemp = multiplier * std::log2(std::max(minRatio, (*srcPtrTemp) * invReferenceMagnitude)); | ||
srcPtrTemp++; | ||
dstPtrTemp++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here on increments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
|
||
srcPtrRow += srcDescPtr->strides.hStride; | ||
dstPtrRow += dstDescPtr->strides.hStride; | ||
} | ||
} | ||
} | ||
|
||
return RPP_SUCCESS; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If user gives multiplier as 0, is there a check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure added this check