Changeset 185

Show
Ignore:
Timestamp:
07/24/08 17:23:32 (4 months ago)
Author:
apdavison
Message:

Added a method to calculate the frequency spectrum of a SpikeTrain. This is a very simple method with no corrections for the finite duration of the spike train, etc., so feel free to improve it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/analysis.py

    r180 r185  
    9696        if D[k] == max_val: 
    9797            return k 
     98         
     99def simple_frequency_spectrum(x): 
     100    """ 
     101    Very simple calculation of frequency spectrum with no detrending, 
     102    windowing, etc. Just the first half (positive frequency components) of 
     103    abs(fft(x)) 
     104    """ 
     105    spec = numpy.absolute(numpy.fft.fft(x)) 
     106    spec = spec[:len(x)/2] # take positive frequency components 
     107    spec /= len(x)         # normalize 
     108    spec *= 2.0            # to get amplitudes of sine components, need to multiply by 2 
     109    spec[0] /= 2.0         # except for the dc component 
     110    return spec 
    98111 
    99112class TuningCurve(object): 
  • trunk/src/signals.py

    r184 r185  
    1010except Exception: 
    1111    print "Warning: pylab not present" 
     12from NeuroTools import analysis 
    1213 
    1314 
     
    120121        return len(self.spike_times) 
    121122 
     123    def duration(self): 
     124        return self.t_stop - self.t_start 
     125 
    122126    def format(self, relative=False, quantized=False): 
    123127        """ 
     
    308312        return tuning_curve 
    309313 
     314    def frequency_spectrum(self, time_bin): 
     315        """ 
     316        Returns the frequency spectrum of the time histogram together with the 
     317        frequency axis. 
     318        """ 
     319        hist = self.time_histogram(time_bin, normalized=False) 
     320        freq_spect = analysis.simple_frequency_spectrum(hist) 
     321        freq_bin = 1000.0/self.duration() # Hz 
     322        freq_axis = numpy.arange(len(freq_spect)) * freq_bin     
     323        return freq_spect, freq_axis     
     324 
     325    def f1f0_ratio(self, time_bin, f_stim): 
     326        """ 
     327        Returns the F1/F0 amplitude ratio where the input stimulus frequency is 
     328        f_stim. 
     329        """ 
     330        freq_spect = self.frequency_spectrum(time_bin)[0] 
     331        F0 = freq_spect[0] 
     332        freq_bin = 1000.0/self.duration() 
     333        try: 
     334            F1 = freq_spect[int(round(f_stim/freq_bin))] 
     335        except IndexError: 
     336            errmsg = "time_bin (%f) too large to estimate F1/F0 ratio for an input frequency of %f" % (time_bin, f_stim) 
     337            errmsg += "\nFrequency_spectrum: %s" % freq_spect 
     338            raise Exception(errmsg) 
     339        return F1/F0 
     340     
    310341 
    311342class SpikeList(object): 
     
    412443            self.spiketrains[id] = spiketrain 
    413444            self.id_list.append(id) 
    414             self.N += 1 
    415445        self.__calc_startstop() 
    416446 
     
    915945    def as_pyNN_SpikeArray(self): 
    916946        """ 
    917         to use in conjonction with SpikeSourceArray neurons 
     947        to use in conjunction with SpikeSourceArray neurons 
    918948 
    919949        >>> pop = Population((10,),SpikeSourceArray, {'spike_times': sl.as_pyNN_SpikeArray() })