From dff2d67154f7d9aca1e5cf6649a5f03f2d948fd5 Mon Sep 17 00:00:00 2001 From: yomguy Date: Wed, 25 Aug 2010 15:20:29 +0000 Subject: [PATCH] add smooth for joy, fix normalize bug --- timeside/grapher/core.py | 110 ++++++++++++++---- .../{waveform_batch => waveform_batch.py} | 11 +- 2 files changed, 94 insertions(+), 27 deletions(-) rename timeside/tools/{waveform_batch => waveform_batch.py} (93%) diff --git a/timeside/grapher/core.py b/timeside/grapher/core.py index 8577871..539ba5b 100644 --- a/timeside/grapher/core.py +++ b/timeside/grapher/core.py @@ -255,57 +255,54 @@ class WaveformImage(object): class WaveformImageJoyContour(WaveformImage): - + def __init__(self, image_width, image_height, nframes, samplerate, fft_size, bg_color, color_scheme, filename=None): WaveformImage.__init__(self, image_width, image_height, nframes, samplerate, fft_size, bg_color, color_scheme, filename=filename) self.contour = numpy.zeros(self.image_width) self.centroids = numpy.zeros(self.image_width) self.ndiv = 6 self.x = numpy.r_[0:self.image_width-1:1] - #self.dx1 = self.x[1]-self.x[0] - self.dx2 = self.x[self.samples_per_pixel/(self.ndiv*10)]-self.x[0] + self.dx1 = self.x[1]-self.x[0] def get_peaks_contour(self, x, peaks, spectral_centroid=None): - """ draw 2 peaks at x using the spectral_centroid for color """ self.contour[x] = numpy.max(peaks) self.centroids[x] = spectral_centroid - + def draw_peaks_contour(self): - contour = cspline1d(self.contour.copy()) - #contour = cspline1d_eval(contour, self.x, dx=self.dx1, x0=self.x[0]) - contour = cspline1d_eval(contour, self.x, dx=self.dx2, x0=self.x[0]) - #print len(contour) - - l_min = min(self.contour) - l_max = max(self.contour) - l_range= l_max - l_min + #contour = self.contour.copy() + contour = smooth(self.contour, window_len=13) - self.contour = (contour-l_min)/l_range - #print contour + l_min = min(contour) + contour = (contour-l_min) + l_max = max(contour) + l_range= l_max - l_min + contour = contour/l_max + contour = cspline1d(contour) + contour = cspline1d_eval(contour, self.x, dx=self.dx1, x0=self.x[0]) # Multispline scales for i in range(0,self.ndiv): self.previous_x, self.previous_y = None, None - bright_color = int(255*(1-float(i)/self.ndiv)) + + #bright_color = 255 + bright_color = int(255*(1-float(i)/(self.ndiv*2))) line_color = (bright_color,bright_color,bright_color) - print line_color - + # Linear #contour = contour*(1.0-float(i)/self.ndiv) #contour = contour*(1-float(i)/self.ndiv) - + # Cosine contour = contour*numpy.arccos(float(i)/self.ndiv)*2/numpy.pi #contour = self.contour*(1-float(i)*numpy.arccos(float(i)/self.ndiv)*2/numpy.pi/self.ndiv) - + # Negative Sine #contour = contour + ((1-contour)*2/numpy.pi*numpy.arcsin(float(i)/self.ndiv)) for j in range(0,self.image_width-1): #line_color = self.color_lookup[int(self.centroids[j]*255.0)] x = self.x[j] - y = contour[j]*self.image_height - #print y + y = contour[j]*(self.image_height-1) if self.previous_y: self.draw.line([self.previous_x, self.previous_y, x, y], line_color) self.draw_anti_aliased_pixels(x, y, y, line_color) @@ -448,3 +445,72 @@ class Noise(object): self.seekpoint += will_read return numpy.random.random(will_read)*2 - 1 + +# TOOLS + +def downsample(vector, factor): + """ + downsample(vector, factor): + Downsample (by averaging) a vector by an integer factor. + """ + if (len(vector) % factor): + print "Length of 'vector' is not divisible by 'factor'=%d!" % factor + return 0 + vector.shape = (len(vector)/factor, factor) + return numpy.mean(vector, axis=1) + + +def smooth(x, window_len=10, window='hanning'): + """smooth the data using a window with requested size. + + This method is based on the convolution of a scaled window with the signal. + The signal is prepared by introducing reflected copies of the signal + (with the window size) in both ends so that transient parts are minimized + in the begining and end part of the output signal. + + input: + x: the input signal + window_len: the dimension of the smoothing window + window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' + flat window will produce a moving average smoothing. + + output: + the smoothed signal + + example: + + import numpy as np + t = numpy.linspace(-2,2,0.1) + x = numpy.sin(t)+numpy.random.randn(len(t))*0.1 + y = smooth(x) + + see also: + + numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve + scipy.signal.lfilter + + TODO: the window parameter could be the window itself if an array instead of a string + """ + + if x.ndim != 1: + raise ValueError, "smooth only accepts 1 dimension arrays." + + if x.size < window_len: + raise ValueError, "Input vector needs to be bigger than window size." + + if window_len < 3: + return x + + if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']: + raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'" + + s=numpy.r_[2*x[0]-x[window_len:1:-1], x, 2*x[-1]-x[-1:-window_len:-1]] + #print(len(s)) + + if window == 'flat': #moving average + w = numpy.ones(window_len,'d') + else: + w = getattr(numpy, window)(window_len) + y = numpy.convolve(w/w.sum(), s, mode='same') + return y[window_len-1:-window_len+1] + diff --git a/timeside/tools/waveform_batch b/timeside/tools/waveform_batch.py similarity index 93% rename from timeside/tools/waveform_batch rename to timeside/tools/waveform_batch.py index ea433c9..8bc8fe0 100644 --- a/timeside/tools/waveform_batch +++ b/timeside/tools/waveform_batch.py @@ -74,16 +74,15 @@ class Media2Waveform(object): if root: for file in files: ext = file.split('.')[-1] - if ext == 'wav' or ext == 'WAV': - media_list.append(root+os.sep+file) + media_list.append(root+os.sep+file) return media_list def get_path_dict(self): path_dict = {} for media in self.media_list: - name = os.path.splitext(media) - name = name[0].split(os.sep)[-1] - path_dict[media] = unicode(self.img_dir + os.sep + name + '.png') + filename = media.split(os.sep)[-1] + name, ext = os.path.splitext(filename) + path_dict[media] = self.img_dir + os.sep + filename + '.png' return path_dict def process(self): @@ -92,8 +91,10 @@ class Media2Waveform(object): print 'Rendering ', source, ' to ', image, '...' audio = os.path.join(os.path.dirname(__file__), source) decoder = timeside.decoder.FileDecoder(audio) + waveform = timeside.grapher.WaveformJoyDiv(width=self.width, height=self.height, output=image, bg_color=self.bg_color, color_scheme=self.color_scheme) + (decoder | waveform).run() print 'frames per pixel = ', waveform.graph.samples_per_pixel waveform.render() -- 2.39.5