1
$\begingroup$

I was given audio with white Gaussian noise and i have to implement two types of filter: Rectangle and Triangle that filter out redundant frequencies and reduce the amount of noise

Here is the code for the audio with the noise

SetDirectory[NotebookDirectory[]];
 Print["Original File:"]
 suite = Import["bach_suite_cello.mp3"] 
suite // Options; 
samples = AudioData[AudioTrim[suite, {2, 13}]][[1]]; 
Print["44k Sampling:"]
 samples44k = Audio[samples, SampleRate -> 44100]
 audioData44k = Flatten[AudioData[samples44k]];
 samples44kWithNoise = audioData44k + RandomVariate[NormalDistribution[0, 0.1], Length[audioData44k]]; 
audioWithNoise = Audio[samples44kWithNoise]

Here's a link to the original audio bach suite cello.

So i want to filter out the noise using Rectangle and Triangle filters

Thanks for all those who help

$\endgroup$

1 Answer 1

4
$\begingroup$
a = ExampleData[{"Audio", "Clapping"}, "Audio"]
(* NOTE: this is mono data.
 For stereo you must apply filtering to both channels [[1]] and [[2]] *)
dat = AudioData[a][[1]];

(* Convolve with your own kernels *)
rectangleFiltered = ListConvolve[{1, 1, 1, 1, 1, 1, 1}/7., dat];
triangleFiltered = ListConvolve[{1, 2, 3, 4, 3, 2, 1}/16., dat];
Audio[rectangleFiltered, SampleRate -> AudioSampleRate[a]]
Audio[triangleFiltered, SampleRate -> AudioSampleRate[a]]

(* See also: Mathematica's built in filtering *)
MeanFilter[a,7]
$\endgroup$
2
  • $\begingroup$ Can you tell me how I get my kernels? Thanks for the help . $\endgroup$
    – omaraw
    Commented Jun 7, 2020 at 16:21
  • $\begingroup$ That's what these are: {1, 1, 1, 1, 1, 1, 1}/7. and {1, 2, 3, 4, 3, 2, 1}/16.. I've also normalized them hence the /7 and /16. The first is flat (rectangular) and the second runs up and then back down (triangular). $\endgroup$
    – flinty
    Commented Jun 7, 2020 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.