Increase volume by X decibels and rewrite audio file - c#

I'm new in naudio. And I wanna increase volume by X db. I've written this piece of code:
public static void IncreaseVolume(string inputPath, string outputPath, double db)
{
double linearScalingRatio = Math.Pow(10d, db / 10d);
using (WaveFileReader reader = new WaveFileReader(inputPath))
{
VolumeWaveProvider16 volumeProvider = new VolumeWaveProvider16(reader);
using (WaveFileWriter writer = new WaveFileWriter(outputPath, reader.WaveFormat))
{
while (true)
{
var frame = reader.ReadNextSampleFrame();
if (frame == null)
break;
writer.WriteSample(frame[0] * (float)linearScalingRatio);
}
}
}
}
Ok, this works, but how can I find by how many decibels I've increased each sample? May anyone explain this moment for me and provide any examples?
UPDATE:
using (WaveFileReader reader = new WaveFileReader(inFile))
{
float Sum = 0f;
for (int i = 0; i < reader.SampleCount; i++)
{
var sample = reader.ReadNextSampleFrame();
Sum += sample[0] * sample[0];
}
var db = 20 * Math.Log10(Math.Sqrt(Sum / reader.SampleCount) / 1);
Console.WriteLine(db);
Console.ReadLine();
}

Your code looks good. To measure the average sound level of an audio sample you need to calculate the RMS (root mean square) of this sound level:
RMS := Sqrt( Sum(x_i*x_i)/N)
with x_i being the i-th sample and N the number of samples. The RMS is the average amplitude of your signal. Use
RMS_dB = 20*log(RMS/ref)
(with ref being 1.0 or 32767.0)
to convert it to a decibel value.
You may calculate this RMS value before and after you change the volume. The difference should be erxactly the dB you used in your IncreaseVolume()

Just adding a comment for people
The input db in line is decibel and you need to convert it into amplitude.
double linearScalingRatio = Math.Pow(10d, db / 10d);
The table is as follows-
https://blog.demofox.org/2015/04/14/decibels-db-and-amplitude/
so you need to provide value as 6 in db, to make it twice as load.
Another point already mentioned it should be
double linearScalingRatio = Math.Pow(10d, db / 20d);

Related

Using Math.NET Numerics is it possible to generate a normally distributed sample with upper and lower bounds?

It's very easy to generate normally distributed data with a desired mean and standard distribution:
IEnumerable<double> sample = MathNet.Numerics.Distributions.Normal.Samples(mean, sd).Take(n);
However with a sufficiently large value for n you will get values miles away from the mean. To put it into context I have a real world data set with mean = 15.93 and sd = 6.84. For this data set it is impossible to have a value over 30 or under 0, but I cannot see a way to add upper and lower bounds to the data that is generated.
I can remove data that falls outside of this range as below, but this results in the mean and SD for the generated sample differing significantly (in my opinion, probably not statistically) from the values I requested.
Normal.Samples(mean, sd).Where(x => x is >= 0 and <= 30).Take(n);
Is there any way to ensure that the values generated fall within a specified range without effecting the mean and SD of the generated data?
The following proposed solution relies on a specific formula for calculating the standard deviation relative to the bounds: the standard deviation has to be a third of the difference between the mean and the required minimum or maximum.
This first code block is the TruncatedNormalDistribution class, which encapsulates MathNet's Normal class. The main technique for making a truncated normal distribution is in the constructor. Note the resulting workaround that is required in the Sample method:
using MathNet.Numerics.Distributions;
public class TruncatedNormalDistribution {
public TruncatedNormalDistribution(double xMin, double xMax) {
XMin = xMin;
XMax = xMax;
double mean = XMin + (XMax - XMin) / 2; // Halfway between minimum and maximum.
// If the standard deviation is a third of the difference between the mean and
// the required minimum or maximum of a normal distribution, 99.7% of samples should
// be in the required range.
double standardDeviation = (mean - XMin) / 3;
Distribution = new Normal(mean, standardDeviation);
}
private Normal Distribution { get; }
private double XMin { get; }
private double XMax { get; }
public double CumulativeDistribution(double x) {
return Distribution.CumulativeDistribution(x);
}
public double Density(double x) {
return Distribution.Density(x);
}
public double Sample() {
// Constrain results lower than XMin or higher than XMax
// to those bounds.
return Math.Clamp(Distribution.Sample(), XMin, XMax);
}
}
And here is a usage example. For a visual representation of the results, open each of the two output CSV files in a spreadsheet, such as Excel, and map its data to a line chart:
// Put the path of the folder where the CSVs will be saved here
const string chartFolderPath =
#"C:\Insert\chart\folder\path\here";
const double xMin = 0;
const double xMax = 100;
var distribution = new TruncatedNormalDistribution(xMin, xMax);
// Densities
var dictionary = new Dictionary<double, double>();
for (double x = xMin; x <= xMax; x += 1) {
dictionary.Add(x, distribution.Density(x));
}
string csvPath = Path.Combine(
chartFolderPath,
$"Truncated Normal Densities, Range {xMin} to {xMax}.csv");
using var writer = new StreamWriter(csvPath);
foreach ((double key, double value) in dictionary) {
writer.WriteLine($"{key},{value}");
}
// Cumulative Distributions
dictionary.Clear();
for (double x = xMin; x <= xMax; x += 1) {
dictionary.Add(x, distribution.CumulativeDistribution(x));
}
csvPath = Path.Combine(
chartFolderPath,
$"Truncated Normal Cumulative Distributions, Range {xMin} to {xMax}.csv");
using var writer2 = new StreamWriter(csvPath);
foreach ((double key, double value) in dictionary) {
writer2.WriteLine($"{key},{value}");
}

How to use the Nelder Meade Simplex algorithm in mathdotnet for function maximization

In my C# program I have a dataset where each data point consists of:
a stimulus intensity (intensity) as x-coordinate
the percentage of correct response (percentageCorrect) to stimulus as y-coordinate
When the intensity is low percentageCorrect is low. When the intensity is high the percentageCorrect is high. The function graph is an S-shaped curve as the percentageCorrect reaches an asymptote at low and high ends.
I am trying to find the threshold intensity where percentageCorrect is half way between the asymtotes at either end (center of the S-shaped curve)
I understand this to be a function maximization problem that can be solved by the Nelder Meade Simplex algorithm.
I am trying to solve my problem using the Nelder Meade Simplex algorithm in mathdotnet and its IObjectiveFunction parameter.
However, I am having trouble understanding the API of the NedlerMeadeSimplex class FindMinimum method and the IObjectiveFunction EvaluateAt method.
I am new to numerical analysis that is pre-requisite for this question.
Specific questions are:
For the NedlerMeadeSimplex class FindMinimum method what are the initialGuess and initialPertubation parameters?
For the IObjectiveFunction EvaluateAt method, what is the point parameter? I vaguely understand that the point parameter is a datum in the dataset being minimized
How can I map my data set to this API and solve my problem?
Thanks for any guidance on this.
The initial guess is a guess at the model parameters.
I've always used the forms that don't require an entry of the initialPertubation parameter, so I can't help you there.
The objective function is what your are trying to minimize. For example, for a least squares fit, it would calculate the sum of squared areas at the point given in the argument. Something like this:
private double SumSqError(Vector<double> v)
{
double err = 0;
for (int i = 0; i < 100; i++)
{
double y_val = v[0] + v[1] * Math.Exp(v[2] * x[i]);
err += Math.Pow(y_val - y[i], 2);
}
return err;
}
You don't have to supply the point. The algorithm does that over and over while searching for the minimum. Note that the subroutine as access to the vector x.
Here is the code for a test program fitting a function to random data:
private void btnMinFit_Click(object sender, EventArgs e)
{
Random RanGen = new Random();
x = new double[100];
y = new double[100];
// fit exponential expression with three parameters
double a = 5.0;
double b = 0.5;
double c = 0.05;
// create data set
for (int i = 0; i < 100; i++) x[i] = 10 + Convert.ToDouble(i) * 90.0 / 99.0; // values span 10 to 100
for (int i = 0; i < 100; i++)
{
double y_val = a + b * Math.Exp(c * x[i]);
y[i] = y_val + 0.1 * RanGen.NextDouble() * y_val; // add error term scaled to y-value
}
// var fphv = new Func<double, double, double, double>((x, A, B) => A * x + B * x + A * B * x * x); extraneous test
var f1 = new Func<Vector<double>, double>(x => LogEval(x));
var obj = ObjectiveFunction.Value(f1);
var solver = new NelderMeadSimplex(1e-5, maximumIterations: 10000);
var initialGuess = new DenseVector(new[] { 3.0, 6.0, 0.6 });
var result = solver.FindMinimum(obj, initialGuess);
Console.WriteLine(result.MinimizingPoint.ToString());
}

NAudio FFT returns small and equal magnitude values for all frequencies

I'm working on a project with NAudio 1.9 and I want to compute an fft for an entire song, i.e split the song in chunks of equal size and compute fft for each chunk. The problem is that NAudio FFT function returns really small and equal values for any freq in the freq spectrum.
I searched for previous related posts but none seemed to help me.
The code that computes FFT using NAudio:
public IList<FrequencySpectrum> Fft(uint windowSize) {
IList<Complex[]> timeDomainChunks = this.SplitInChunks(this.audioContent, windowSize);
return timeDomainChunks.Select(this.ToFrequencySpectrum).ToList();
}
private IList<Complex[]> SplitInChunks(float[] audioContent, uint chunkSize) {
IList<Complex[]> splittedContent = new List<Complex[]>();
for (uint k = 0; k < audioContent.Length; k += chunkSize) {
long size = k + chunkSize < audioContent.Length ? chunkSize : audioContent.Length - k;
Complex[] chunk = new Complex[size];
for (int i = 0; i < chunk.Length; i++) {
//i've tried windowing here but didn't seem to help me
chunk[i].X = audioContent[k + i];
chunk[i].Y = 0;
}
splittedContent.Add(chunk);
}
return splittedContent;
}
private FrequencySpectrum ToFrequencySpectrum(Complex[] timeDomain) {
int m = (int) Math.Log(timeDomain.Length, 2);
//true = forward fft
FastFourierTransform.FFT(true, m, timeDomain);
return new FrequencySpectrum(timeDomain, 44100);
}
The FrequencySpectrum:
public struct FrequencySpectrum {
private readonly Complex[] frequencyDomain;
private readonly uint samplingFrequency;
public FrequencySpectrum(Complex[] frequencyDomain, uint samplingFrequency) {
if (frequencyDomain.Length == 0) {
throw new ArgumentException("Argument value must be greater than 0", nameof(frequencyDomain));
}
if (samplingFrequency == 0) {
throw new ArgumentException("Argument value must be greater than 0", nameof(samplingFrequency));
}
this.frequencyDomain = frequencyDomain;
this.samplingFrequency = samplingFrequency;
}
//returns magnitude for freq
public float this[uint freq] {
get {
if (freq >= this.samplingFrequency) {
throw new IndexOutOfRangeException();
}
//find corresponding bin
float k = freq / ((float) this.samplingFrequency / this.FftWindowSize);
Complex c = this.frequencyDomain[checked((uint) k)];
return (float) Math.Sqrt(c.X * c.X + c.Y * c.Y);
}
}
}
for a file that contains a sine wave of 440Hz
expected output: values like 0.5 for freq=440 and 0 for the others
actual output: values like 0.000168153987f for any freq in the spectrum
It seems that I made 4 mistakes:
1) Here I'm asumming that sampling freq is 44100. This was not the reason my code wasn't working, though
return new FrequencySpectrum(timeDomain, 44100);
2) Always make a visual representation of your output data! I must learn this lesson... It seems that for a file containing a 440Hz sine wave I'm getting the right result but...
3) The frequency spectrum is a little shifted from what I was expecting because of this:
int m = (int) Math.Log(timeDomain.Length, 2);
FastFourierTransform.FFT(true, m, timeDomain);
timeDomain is an array of size 44100 becaused that's the value of windowSize (I called the method with windowSize = 44100), but FFT method expects a window size with a value power of 2. I'm saying "Here, NAudio, compute me the fft of this array that has 44100 elements, but take into account only the first 32768". I didn't realize that this was going to have serious implications on the result:
float k = freq / ((float) this.samplingFrequency / this.FftWindowSize);
Here this.FftWindowSize is a property based on the size of the array, not on m. So, after visualizing the result I found out that magnitude of 440Hz freq was actually corresponding to the call:
spectrum[371]
instead of
spectrum[440]
So, my mistake was that the window size of fft (m) was not corresponding to the actual length of the array (FrequencySpectrum.FftWindowSize).
4) The small values that I was receiving for the magnitudes came from the fact that the audio file on which I was testing my code wasn't recorded with enough gain.

FFT Fundamental frequency calculation from LomontFFT

I am using the LomontFFT from http://www.lomont.org/Software/Misc/FFT/LomontFFT.html to get the fundamental frequency from sampled values of a signal. For testing if the fundamental frequency is correctly determined, I have used some samples from past (with known fundamental frequency).
Below is the code I have written to call the LomontFFT algo and determine the FFT:
private void buttonFFT_Click(object sender, EventArgs e)
{
//double fftavg = 0;
double fftmax = 0;
var fftData = new byte[512];
double[] fftValues = Enumerable.Repeat(0.0, 512).ToArray();
Array.Copy(sapmledDoubleValuesADC1, fftValues, sapmledDoubleValuesADC1.Length);
var fftMethod = new Lomont.LomontFFT();
fftMethod.RealFFT(fftValues, true);
for (int i = 0; i < 512; i += 2)
{
double fftmag = Math.Sqrt((fftValues[i] * fftValues[i]) + (fftValues[i + 1] * fftValues[i + 1]));
if (fftmag > fftmax)
fftmax = fftmag;
//fftavg += fftmag;
//fftData[i] = (byte)fftmag;
//fftData[i + 1] = fftData[i];
}
textBoxFundaFreq.Text = "Frey = " + fftmax.ToString();
for (int x = 1; x < 512; x++)
{
this.chart2.Series[0].Points.AddXY(x, fftValues[x]);
}
}
But the problem is the magnitude of the frequency is wrong. The FFT also doesn't match but that is possible as there are multiple solutions, but the frequency should be same. The algo is proven for many years so its definitely not wrong. Am I doing something wrong in calling code?
(I have only real values in sampled data)
Fundamental frequency extraction / pitch detection is not a simple algorithm. For most input signal (anything other than a single sine/cosine wave) the FFT will show several peaks and it is usually better to estimate the distance between this peaks.
Further you need to interpolate the FFT bins to get an acurate result for a single peak.
For most applications it is better to calculate the auto-correlation-function (ACF) anyway.

How can I resample wav file

Currently I'm recording an audio signal with following specs:
Channels: 1
SamplesPerSecond: 8000
BitsPerSample: 16
How can I convert this .wav-file to eg following specs (pure c# is preferred):
Channels: 1
SamplesPerSecond: 22050
BitsPerSample: 16
Windows API (one of) to resample audio is Audio Resampler DSP. This transform class is pretty straightforward to set up input and output types, then push input data and pull output.
Another task you would possible deal additionally with is reading from file and writing into a new file (you did not specify if it is actually needed in your original description though).
You might also want to use third party libraries like NAudio.
See also:
C# resample audio from 8khz to 44.1/48khz
Audio DSP in C#
try Naudio - it is a free + opensource .NET library offering several things including the ability to resample AFAIK.
As requested sample source for resampling
AS3 function for resampling. You can easy change to convert this code to C#:
private function resampling(fromSampleRate:int, toSampleRate:int, quality:int = 10):void
{
var samples:Vector.<Number> = new Vector.<Number>;
var srcLength:uint = this._samples.length;
var destLength:uint = this._samples.length*toSampleRate/fromSampleRate;
var dx:Number = srcLength/destLength;
// fmax : nyqist half of destination sampleRate
// fmax / fsr = 0.5;
var fmaxDivSR:Number = 0.5;
var r_g:Number = 2 * fmaxDivSR;
// Quality is half the window width
var wndWidth2:int = quality;
var wndWidth:int = quality*2;
var x:Number = 0;
var i:uint, j:uint;
var r_y:Number;
var tau:int;
var r_w:Number;
var r_a:Number;
var r_snc:Number;
for (i=0;i<destLength;++i)
{
r_y = 0.0;
for (tau=-wndWidth2;tau < wndWidth2;++tau)
{
// input sample index
j = (int)(x+tau);
// Hann Window. Scale and calculate sinc
r_w = 0.5 - 0.5 * Math.cos(2*Math.PI*(0.5 + (j-x)/wndWidth));
r_a = 2*Math.PI*(j-x)*fmaxDivSR;
r_snc = 1.0;
if (r_a != 0)
r_snc = Math.sin(r_a)/r_a;
if ((j >= 0) && (j < srcLength))
{
r_y += r_g * r_w * r_snc * this._samples[j];
}
}
samples[i] = r_y;
x += dx;
}
this._samples = samples.concat();
samples.length = 0;
}
Try code below from C# resample audio from 8khz to 44.1/48khz
static void Resample(string fileName)
{
IntPtr formatNew = AudioCompressionManager.GetPcmFormat(2, 16, 44100);
WaveReader wr = new WaveReader(File.OpenRead(fileName));
IntPtr format = wr.ReadFormat();
byte[] data = wr.ReadData();
wr.Close();
//PCM 8000 Hz -> PCM 44100
byte[] dataNew = AudioCompressionManager.Resample(format, data, formatNew);
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),
AudioCompressionManager.FormatBytes(formatNew));
ww.WriteData(dataNew);
ww.Close();
}

Categories

Resources