Nearest Neighbor Upsampling

Using Nyquist scripts in Audacity.
Post and download new plug-ins.

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

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by greenblock » Thu Oct 18, 2012 9:38 pm

Thanks!

To tell the truth... I think using a highpass filter on what this generates and combining with other interpolation methods may be the best... seriously! Getting useful high frequencies from those plugins you sent me seems a lot harder and most of the time they sound like normal clipping, ahhaha.

If you're curious about what I needed this for... I ripped the Megarace OST from the 3D0 version, but it's 11025Hz and as it has lots of phasers, distortions, reverse cymbal-like effects, so it'll sound ok after a bit of work and eq.

See you :)

Ps. I changed bad-resample to awesome-resample in your code, now it's fitting :P
greenblock
 
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by Robert J. H. » Thu Oct 18, 2012 11:23 pm

I didn't test Steve's code but I think the approach with snd-fetch within loops may be a little bit slow. Here's another solution without the use of arrays:
Code: Select all
;; resample nearest neighbour
(defun smp-map (factor dur)
   (let* (
   (freq (hz-to-step (/ *sound-srate* factor)))
   (smp (/ *sound-srate*))
   (*nigh-table* (maketable (abs-env (seq
      (snd-const 1 0 *sound-srate* smp)
      (s-rest (* smp (1- *sound-srate*))))))))
   (integrate (abs-env (osc freq dur  *nigh-table*)))
)); end smp-map

(setf factor 4)
(snd-compose s (smp-map factor  (get-duration factor)))

factor means of course the stretch factor or how many times a sample is repeated. The *nigh-table* map offers a lot of interesting variations. at a factor of 4, the resulting oscilator function will produce "10001000...). A value of one means "take the next sample" and 0 means "repeat the last sample". there are other combinations possible. It's even possible to produce a sample order like: "2 1 3 2 4 3...". I have no idea how this will sound like.
Optionally, the sound can immediately be resampled to the original sample rate, thus producing a little amount of the metallic character of this sample method.
Robert J. H.
 
Posts: 1813
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 7

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by steve » Fri Oct 19, 2012 12:15 am

Robert J. H. wrote:I didn't test Steve's code but I think the approach with snd-fetch within loops may be a little bit slow.

Definitely so. Quick and dirty programming :D
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 47011
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by Robert J. H. » Fri Oct 19, 2012 1:12 am

steve wrote:
Robert J. H. wrote:I didn't test Steve's code but I think the approach with snd-fetch within loops may be a little bit slow.

Definitely so. Quick and dirty programming :D


And I've been trying to circumvent such a blunt comment... :D

Here's a variation from above which swaps the samples in pairs (021324354...). As expected, the influence is most prominent at frequencies near half of the sample-rate. The higher the factor, the more the effect is audible but the clipping danger increases also.
Besides, my code isn't very efficient either. The solution to the original problem could be written in a single line (but less flexibel though).
Code: Select all
;; Swapping samples
(defun smp-map (factor dur)
   (let* (
   (freq (hz-to-step (/ *sound-srate* factor)))
   (smp (/ *sound-srate*))
   (*nigh-table* (maketable (abs-env (seq
      (snd-const factor  0 *sound-srate* smp)
      (snd-const -1 0 *sound-srate*
      (* smp (1- *sound-srate*))))))))
   (integrate (abs-env (osc freq dur  *nigh-table*)))
)); end smp-map

(setf factor 2)
(mult 0.9 (force-srate (/ *sound-srate* factor)
(snd-compose s (smp-map factor  (get-duration factor)))))
;(snd-display (mult 44100 (smp-map factor   0.001)))
Robert J. H.
 
Posts: 1813
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 7

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by steve » Fri Oct 19, 2012 2:29 am

Robert J. H. wrote:Optionally, the sound can immediately be resampled to the original sample rate,

But doesn't that negate the effect by capping the frequency band below the generated frequencies?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 47011
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by Robert J. H. » Fri Oct 19, 2012 5:02 am

how much of the metalic effect is preserved during the back sampling depends largely on the resampling method used.
It is clear that a harmonic spectrum that results from added new samples can't fully be expressed by only one remaining sample. Nevertheless, the interpolation will be weighted in a different way. I think that the second code above, where the samples are swaped produces a much stronger effect when the sample rate remains the same. It's presumably only a kind of resonant filter in the high frequencies.

Here's the shortest code snippet for the resampling that I was able to develop so far:
Code: Select all
(setf l (truncate (* len (psetq sr *sound-srate* factor 2))))
(multichan-expand #'snd-compose s (mult (/ sr) (quantize (snd-pwl 0 sr(list l len l)) 1)))

It isn't likely that it will win a beauty prize. But I grandiously boasted above that all could be squeezed into one line...;)
Robert J. H.
 
Posts: 1813
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 7

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by greenblock » Fri Oct 19, 2012 7:12 am

Wow! Your version is very fast o.o

Programming is funny, squeezing big things into small things.

Now you need to officialize it and make a '.ny' <3 So we can install it instead of having a TXT in our desktops hahahahahahah.

But now I started to search about Sinc interpolation. On images it looks a bit better than bicubic http://videoprocessing.ucsd.edu/~dungvo/DataPrune.html

So why people consider it to be the best for upsampling audio (I'm not talking about brightness of sound this time), all audio editors have it as the 'best'!
greenblock
 
Posts: 8
Joined: Tue Oct 16, 2012 9:01 pm
Operating System: Please select

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by Robert J. H. » Fri Oct 19, 2012 11:07 am

Resampling is certainly an interesting field of research. The SINC-interpolation yields the best results because it introduces the least new frequencies while retaining the original spectrum. However, the ideal anti-aliasing filter doesn't exist for discreet/finite signals. It is therefore the truncation or windowing of the SINC-function that makes the big difference in the numerous plug-ins and software products.
Our method from above elegantly circumvents the problem of anti-aliasing by simply allowing it...(that's the back-folding of the high frequencies). Furthermore, the integrety of the frequency-response is discarded in respect to normal procedures, where the samples are either linearly interpolated (seldom) or simply filled with zeros (general case) prior to the filter application.

It would be interesting to unite different resampling (and dithering) methods in a Nyquist-plug-in. It's a pity that I am no mathematician. Filter design gives me the creeps. ;)
Robert J. H.
 
Posts: 1813
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 7

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by steve » Fri Oct 19, 2012 4:24 pm

Robert J. H. wrote:It isn't likely that it will win a beauty prize. But I grandiously boasted above that all could be squeezed into one line...;)

A nice idea to use SND-COMPOSE with QUANTIZE.
A slightly modified version in one line - I think the modern expression is "fugly" ;)
Code: Select all
(setq factor 4)

(multichan-expand #'snd-compose s (mult (/ *sound-srate*)(quantize (snd-pwl 0 *sound-srate* (list (truncate (* len factor)) len (truncate (* len factor)))) 1)))


Perhaps the most "elegant" solution I've found:
Code: Select all
(setq factor 4)

(control-srate-abs *sound-srate*
  (let ((sig2 (mult (/ *sound-srate*)
                (quantize (pwl factor len factor) 1))))
    (multichan-expand #'snd-compose s sig2)))


greenblock wrote:Now you need to officialize it and make a '.ny' <3 So we can install it instead of having a TXT in our desktops hahahahahahah.

I'm not sure about "officializing" it :) but here's a plug-in. I've called the effect "Repeat Samples..."
Attachments
RepeatSamples.ny
(621 Bytes) Downloaded 101 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 47011
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Nearest Neighbor Upsampling

Permanent link to this post Posted by steve » Fri Oct 19, 2012 4:26 pm

Robert J. H. wrote:Filter design gives me the creeps. ;)

There's a very nice plug-in here that takes much of the sweat out of it: viewtopic.php?p=178369#p178369
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 47011
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

PreviousNext

Return to Nyquist



Who is online

Users browsing this forum: No registered users and 2 guests