Module Expr Str Import alias identifier Import alias identifier Import alias identifier Import alias identifier Import alias identifier Expr Str Assign TupleStore NameStore NameStore NameStore Call AttributeLoad NameLoad attr keyword Num keyword Num AugAssignSub NameStore Call AttributeLoad NameLoad attr Expr Str Assign NameStore Num Assign NameStore ListLoad Assign NameStore ListLoad Assign NameStore ListLoad Expr Str For TupleStore NameStore NameStore Call NameLoad ListLoad Num Num Num Num body Expr Str Expr Call AttributeLoad NameLoad attr BinOpAdd Call AttributeLoad NameLoad attr TupleLoad NameLoad SubscriptLoad AttributeLoad NameLoad attr Index Num NameLoad Assign NameStore SubscriptLoad AttributeLoad SubscriptLoad NameLoad Index Num attr Index Num Expr Str For NameStore Call NameLoad NameLoad body AugAssignAdd SubscriptStore SubscriptLoad NameLoad Index Num Index NameLoad BinOpMult Call AttributeLoad AttributeLoad NameLoad attr attr SubscriptLoad AttributeLoad SubscriptLoad SubscriptLoad NameLoad Index Num Index NameLoad attr Index Num NameLoad Expr Str Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num Num Expr Str Expr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad SubscriptLoad NameLoad Index Num attr Num Num Num Expr Call AttributeLoad NameLoad attr AttributeLoad SubscriptLoad NameLoad Index Num attr Expr Call AttributeLoad NameLoad attr NameLoad Str keyword Num Expr Call AttributeLoad NameLoad attr NameLoad Str keyword Num Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Str Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num keyword Num Expr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad Expr Str Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num keyword Num Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num keyword Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Expr Str
N/A N/A 
==============================================
Caclulation of Signal to noise and information
==============================================

This method is based on ideas described in [Borst1999]_ (Figure 2) and
[Hsu2004]_. The calculation can be used, for example, in order to estimate the
channel capacity of a neuron responding to a repeated stimulus.

The estimate of the information is based on the formula


.. math::

      I(S,R) = \int_{0}^{Nyquist}log_2(1+SNR(\omega))d\omega


Where $SNR(\omega)$ is the ratio of the signal power and the noise power at the
frequency band centered on $\omega$.This equation holde true for a Gaussian
channel and is an upper bound for all other cases.

The signal power is estimated as the power of the mean response to repeated
presentations of the same signal and the noise power is calculated as the
average of the power in the deviation from this average in each trial

We import the neccesary modules:

 N/A numpy np N/A matplotlib.pyplot plt N/A nitime.utils utils N/A nitime.timeseries ts N/A nitime.viz viz N/A 

For this example, we generate an auto-regressive sequence to be the signal:

 N/A N/A ar_seq nz alpha N/A N/A utils ar_generator N 128 drop_transients 10 N/A ar_seq N/A N/A ar_seq mean N/A 

The signal will be repeated several times, adding noise to the signal in each
repetition:

 N/A n_trials 12 N/A fig_snr N/A N/A sample N/A N/A fig_tseries N/A N/A 

We add different levels of noise to the ar_seq variable, in order to
demonstrate the effects of adding noise on signal to noise ratio, as well as
the calculated information

 N/A N/A idx noise N/A enumerate N/A 1 10 50 100 N/A N/A 

    Make n_trials repetitions of the signal:

     N/A N/A N/A sample append N/A N/A N/A np ones N/A n_trials N/A N/A ar_seq shape N/A -1 ar_seq N/A n_points N/A N/A N/A sample N/A -1 shape N/A -1 N/A 

    Add noise:

     N/A trial N/A range n_trials N/A N/A N/A N/A sample N/A -1 N/A trial N/A N/A N/A N/A np random randn N/A N/A N/A N/A sample N/A -1 N/A trial shape N/A 0 noise N/A 

    This is the estimate of the signal:

     N/A sample_mean N/A N/A np mean N/A sample N/A -1 0 N/A 

    We plot a comparison of the actual signal (blue) and this estimate(blue). The
    thinner lines n other colors, represent the individual trials:

     N/A N/A N/A fig_tseries append N/A N/A plt figure N/A ax N/A N/A N/A fig_tseries N/A -1 add_subplot 1 1 1 N/A N/A N/A ax plot N/A N/A sample N/A -1 T N/A N/A N/A ax plot ar_seq b linewidth 4 N/A N/A N/A ax plot sample_mean r linewidth 4 N/A N/A N/A ax set_xlabel Time N/A N/A N/A ax set_ylabel Amplitude N/A 

    We present this at different levels of noise. With low noise, the estimate
    of the signal and also the response of the system at different repetitions
    is very similar to the original signal.

     N/A tseries N/A N/A ts TimeSeries N/A sample N/A -1 sampling_rate 1.0 N/A N/A N/A fig_snr append N/A N/A viz plot_snr tseries N/A 

.. image:: fig/snr_example_01.png

A special visualization function :func:`viz.plot_snr` is used in order to
display the signal power (blue) and the noise power (green), both in the
left sub-plot. In addition, the SNR (blue) and the cumulative information
(as a function of frequency bands, starting from low frequencies, in red)
are dislplayed in the right subplot.


.. image:: fig/snr_example_02.png

With more added noise, the estimate of the signal deviates further from the
signal.

.. image:: fig/snr_example_03.png

The signal power remains rather similar, but the noise power increases
(across all bands). As a consequence, the signal to noise ratio decreases and the
accumulated information decreases

.. image:: fig/snr_example_04.png

This becomes even more apparent with more noise:

.. image:: fig/snr_example_05.png


.. image:: fig/snr_example_06.png


Until, with the largest amplitued of noise, the signal power is almost completely
overwhelmed with noise:

.. image:: fig/snr_example_07.png

.. image:: fig/snr_example_08.png

Finally, we use :func:`plot_snr_diff` in order to compare information
transmission (on the left) and the signal to noise ratio (on the right) between
the two last noise levels:
 N/A ts1 N/A N/A ts TimeSeries N/A sample N/A -1 sampling_rate 1.0 N/A ts2 N/A N/A ts TimeSeries N/A sample N/A -2 sampling_rate 1.0 N/A fig_compare N/A N/A viz plot_snr_diff ts1 ts2 N/A N/A N/A plt show N/A 

.. image:: fig/snr_example_09.png


References

    .. [Hsu2004] Hsu A, Borst A and Theunissen, FE (2004) Quantifying
    variability in neural responses ans its application for the validation of
    model predictions. Network: Comput Neural Syst 15:91-109

    .. [Borst1999] Borst A and Theunissen FE (1999) Information theory and
    neural coding. Nat Neurosci 2:947-957


