Nearest Neighbor Upsampling

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
greenblock
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Nearest Neighbor Upsampling

Post by greenblock » Tue Oct 16, 2012 9:47 pm

Hello.

My name's Pedro and I'm in need of a better upsampling method for low quality files. I've tried many and I prefer that one! I call it nearest neighbor because it apparently repeats each sample, so you can only upsample two times, three times, four times, etc. the original sampling rate!

I have absolutely 0 knowledge on audio programming and only a tiny bit about how audio works.

Anyway, I'm only requesting this because my friend wrote a program on DOS that does this but never made it user friendly, for one to use it, the source must be edited to set the conversion parameters and run it. So everytime I have to ask him to upsample my files XD.

What I envisioned was a window with a text field where we could enter a Natural number meaning the number of times a sample would be repeated.

Here http://www.mediafire.com/?6cka2kdskhxg9pu is a 7z file with many upsampling methods (Original File, Polynomial Interpolation, Audacity Resample feature, Audition Resample Feature and Nearest Neighbor Upsample).
Last edited by greenblock on Tue Oct 16, 2012 11:40 pm, edited 1 time in total.

steve
Site Admin
Posts: 47281
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Nearest Neighbor Upsampling

Post by steve » Tue Oct 16, 2012 11:32 pm

"Nearest Neighbour" up-sampling could be done using Nyquist and Audacity, but why would you want to? The sound quality of this method is terrible compared to other methods.

If this is only as an academic exercise, then I can show you a simple but very slow way of doing it.
If this is intended as a practical method for resampling then I would suggest that you use some other method, such as (in Audacity) "Tracks menu > Resample".
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

greenblock
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Post by greenblock » Tue Oct 16, 2012 11:39 pm

Steve,

Thanks for the response. But to be frank, I do think it sounds better than the other methods! In the compressed file I posted I made many .WAV files I made using many resampling techniques and I really like the nearest neighbor interpolation!

I'd just like to have it as no program has it as an option :*(.

steve
Site Admin
Posts: 47281
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Nearest Neighbor Upsampling

Post by steve » Wed Oct 17, 2012 2:54 am

greenblock wrote: But to be frank, I do think it sounds better than the other methods!
The result is "brighter" sounding than other methods because of the distortion that it creates.

Anyway, I thought it was an interesting exercise, so I've written some code for you.

One thing that Nyquist cannot do is to change the track sample rate, so you will need to do that manually.
To change the track sample rate, use the track drop down menu. See here: http://manual.audacityteam.org/o/man/tr ... _menu.html The Sample Rate settings are described at the bottom of the page.

So let's say that you want to up-sample using the "nearest neighbour" method by a factor of 3:
If the track sample rate it, say, 8000 Hz, then you will need to change it to 3 x 8000 = 24000 Hz. (the track will now sound high pitched and speeded up).

Next we will do the resampling.
From the Effect menu, select the Nyquist Prompt effect.
Copy and paste this code into the Nyquist Prompt text box:

Code: Select all

(setq multiplier 3) ; an integer


(defun bad-resample (sig num)
  (setq size 1000)
  (setf s-array (make-array (* num size)))
  (setf end-array
    (if (/= (rem (truncate len) size) 0)
      (make-array (* num (rem (truncate len) size)))
      nil))
  (setf output (s-rest 0))
  (dotimes (count (truncate (/ len size)))
    (fill-array s-array sig num)
    (setf output
      (sim
        output
        (at-abs (/ (* count num size) *sound-srate*)
          (cue (snd-from-array 0 *sound-srate* s-array))))))
  (if end-array
    (progn
      (fill-array end-array sig num)
      (sim
        output
        (at-abs (/ (* (truncate (/ len size)) (* num size)) *sound-srate*)
          (cue (snd-from-array 0 *sound-srate* end-array)))))
    output))

(defun fill-array (s-a sig times)
  (dotimes (count (/ (length s-a) times))
    (let ((next (snd-fetch sig)))
      (dotimes (i times)
        (setf (aref s-a (+ i (* count times))) next)))))

(multichan-expand #'bad-resample s (truncate multiplier))
Apply the effect.

Note that the first line sets the "upsampling" ratio. If you want to upsample 4 times, change:

Code: Select all

(setq multiplier 3) ; an integer
to:

Code: Select all

(setq multiplier 4) ; an integer
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

greenblock
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Post by greenblock » Wed Oct 17, 2012 11:58 pm

Wow! I tested it!
Even though you are right about the distortion... it doesn't 'add' it in my understanding... It's just like an enlarged pixelated image. While it was not meant to be all blocky, it's a limit of it's original resolution.

Thanks a lot, really!

steve
Site Admin
Posts: 47281
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Nearest Neighbor Upsampling

Post by steve » Thu Oct 18, 2012 1:23 am

greenblock wrote: it doesn't 'add' it in my understanding It's just like an enlarged pixelated image. While it was not meant to be all blocky, it's a limit of it's original resolution.
Which one is "blockier"?

Before "up-sampling":
firsttrack000.png
firsttrack000.png (9.12 KiB) Viewed 5453 times
After up-sampling:
firsttrack001.png
firsttrack001.png (8.61 KiB) Viewed 5453 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

greenblock
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Post by greenblock » Thu Oct 18, 2012 2:16 am

Well we can do that with images too:

Image

There are many upscaling algorythms! Depends on the case, really, If I had to upsample a song with voices I'd use polynomial interpolation instead as It has less noise.

steve
Site Admin
Posts: 47281
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Nearest Neighbor Upsampling

Post by steve » Thu Oct 18, 2012 4:07 am

The difference between your graphic example and the audio processing is that the graphic up-sampling is reducing the appearance of "steps", whereas the "Nearest Neighbour up-sampling" is increasing the appearance of steps.

Back to the audio sample:
Where the original sample rate is reasonably high (44.1 kHz or above), the "Nearest Neighbour up-sampling" does not do much harm as the distortion frequencies are all too high to hear.

If the original audio has a low sample rate, then "Nearest Neighbour up-sampling" will create audible distortion.

If audio up-sampling is done perfectly, the result should sound identical to the original.

Here are three audio samples.
The first is the low sample rate original:
original.wav
(1.03 MiB) Downloaded 201 times

The second has been (almost perfectly) up-sampled (using "Very High Quality Sync Interpolation"). You will notice that it sounds identical to the original.
up-sampled.wav
(767 KiB) Downloaded 190 times
The third has been processed with a multiplication factor of 4
processed.wav
(767 KiB) Downloaded 182 times
The "distortion" can be clearly heard as additional high frequencies. It certainly makes the sound "brighter" but with something of a metallic sound to it.

The difference can also be seen it the spectrum.
The upper track shows the spectrum of the original audio - note how the frequencies only go up to 4 kHz.
The lower track shows the spectrum of the processed audio - note how frequencies have been added from 4 kHz right up to 16 kHz.
tracks000.png
tracks000.png (179.58 KiB) Viewed 5440 times
I'm not saying that there is anything "wrong" with this process as "an effect". I'm just saying that it should not be confused with "high quality up-sampling" ;)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

greenblock
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Post by greenblock » Thu Oct 18, 2012 6:11 pm

Yeah, I know it sounds metallic but depending on the thing you're gonna upsample you're upsampling it actually doesn't sound bad!

I'm my case it was an electric guitar with a phaser! hahah Sounded very 'fitting'.

But get what you are saying and I agree too, but do you have any suggestion in making LQ sound sounding better? Like, I'm a graphic designer and I can understand images, sound is somewhat similar but has some weird differences ahha.

That upsample is weird because it simply flips the upper part of the spectrum and repeats it.. Does any program can create frequencies that were not there before?

Like...

This:

Image

But as I think about it, it seems it's not like that example but more like 'a part of the image is missing'. And that I know is a totally different situation.

steve
Site Admin
Posts: 47281
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu
Contact:

Re: Nearest Neighbor Upsampling

Post by steve » Thu Oct 18, 2012 6:27 pm

greenblock wrote: Yeah, I know it sounds metallic but depending on the thing you're gonna upsample you're upsampling it actually doesn't sound bad!

I'm my case it was an electric guitar with a phaser! hahah Sounded very 'fitting'.
Yes I can imagine that.

As I said, it was an interesting exercise, and the sound of the distortion is quite unique - I may even turn this into a kind of "distortion effect" ;)
greenblock wrote:That upsample is weird because it simply flips the upper part of the spectrum and repeats it
Ah yes, you noticed that too. Weird isn't it.
greenblock wrote:Does any program can create frequencies that were not there before?
You mean like "high frequency selective enhancement"?
A technique that was sometimes used in the old analogue days (particularly for drums) was to slightly overload the recording tape ("tape saturation"). This causes a kind of "soft distortion" (as well as "dynamics compression").
Most forms of distortion add upper harmonics to the sound - the difficulty is in finding a distortion that creates "pleasant" upper harmonics.

There are two plug-ins here that you could try:
http://forum.audacityteam.org/viewtopic ... 77#p132477
http://forum.audacityteam.org/viewtopic ... 11#p133311
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply