From: Sow-Mun Chan on
Hi, I have found many threads that had the same problem. Unfortunately, I am not that familiar with Matlab so I wasn't able to fix it. My wrote represents the ear, and what the neurons do when a signal gets through - there will be a spike. I have a few loops in it, that count how often a signal gets through, etc. Now I want to draw a period histogram that shows me at what phase of the sine wave the most spikes are.
I've written the code and now it tells me "Subscript indices must either be real positive integers or logicals." The error is in the line y = a * sin (x) + b * (randn(1,length(x)));
Can anybody help me with this issue?
Thank you in advance.

Sow-Mun

d = 10;
x = 1:(1/d):300;
a = 1;
b = 2;
f = 1000;
%f = frequency

y = a * sin (x) + b * (randn(1,length(x)));
%sine wave with amplitude a, noise with size b

count = zeros(1,100);
period = zeros(1,60);

for j = 1:100


c=0.002;
%c=refractory period5
k=zeros(1,length(x));


%d needed due to 0.1 steps
for i = 1:length(x)

c=c+((1/(f*d))/(2*pi));
%time step

if y(i)>1 && c>=0.002

k(i)=1;
% if y(i)>1 and the refractory period is equal to to or bigger than
% 0.002 then a signal is detected
c=0;
end

end

c = sum (k);
count(c+1) = count(c+1) + 1;


phase = 0;
k = find(k);
%find positions of spikes
for v = 1:length(k)
phase(v) = mod(x(k(v)), 2*pi);
%find x-value at position of spike
end

bins = round(phase/(2*pi)*60);
%round the number to integer so they can be allocated to a bin
bins = bins(bins > 0);
%remove all zeros

for length = 1:length(bins)
period(bins(length)) = period(bins(length)) + 1;

end


end


count
time = x*(1/f)/(2*pi);
plot (time, y)
figure
plot (time, k)
figure
plot(time, y, time, k, 'red' );
xlabel('time')
ylabel('amplitude')
figure
plot(period, '.');
From: Nathan on
On Jan 20, 4:35 pm, "Sow-Mun Chan" <sow.c...(a)warwick.ac.uk> wrote:
> Hi, I have found many threads that had the same problem. Unfortunately, I am not that familiar with Matlab so I wasn't able to fix it. My wrote represents the ear, and what the neurons do when a signal gets through - there will be a spike. I have a few loops in it, that count how often a signal gets through, etc. Now I want to draw a period histogram that shows me at what phase of the sine wave the most spikes are.
> I've written the code and now it tells me "Subscript indices must either be real positive integers or logicals." The error is in the line y = a * sin (x) + b * (randn(1,length(x)));
> Can anybody help me with this issue?
> Thank you in advance.
>
> Sow-Mun
>
> d = 10;
> x = 1:(1/d):300;
> a = 1;
> b = 2;
> f = 1000;
> %f = frequency
>
> y = a * sin (x) + b * (randn(1,length(x)));
>     %sine wave with amplitude a, noise with size b  
>
> count = zeros(1,100);
> period = zeros(1,60);
>
> for j = 1:100
>
>     c=0.002;
>     %c=refractory period5
>     k=zeros(1,length(x));
>
>     %d needed due to 0.1 steps
>     for i = 1:length(x)
>
>         c=c+((1/(f*d))/(2*pi));
>         %time step
>
>         if y(i)>1  && c>=0.002
>
>             k(i)=1;
>             % if y(i)>1 and the refractory period is equal to to or bigger than
>             % 0.002 then a signal is detected
>             c=0;
>         end
>
>     end
>
>     c = sum (k);
>     count(c+1) = count(c+1) + 1;
>
>     phase = 0;
>     k = find(k);
>     %find positions of spikes
>     for v = 1:length(k)
>         phase(v) = mod(x(k(v)), 2*pi);
>         %find x-value at position of spike
>     end
>
>     bins = round(phase/(2*pi)*60);
>     %round the number to integer so they can be allocated to a bin
>     bins = bins(bins > 0);
>     %remove all zeros
>
>     for length = 1:length(bins)
>         period(bins(length)) = period(bins(length)) + 1;
>
>     end
>
> end
>
>   count
> time = x*(1/f)/(2*pi);
> plot (time, y)
> figure
> plot (time, k)
> figure
> plot(time, y, time, k, 'red' );
> xlabel('time')
> ylabel('amplitude')
> figure
> plot(period, '.');

Your problem seems that your code is riddled with errors.

One major thing you need to notice:
You created a variable called length within one of your loops.
Doing so, you can no longer use the function LENGTH within your mfile.
NEVER USE FUNCTION NAMES AS VARIABLES.
I cannot stress that enough.
Change this, and then go through your code to see what you want done.

Another error I see is that you are trying to index your variable
count by a non-integer variable c. As the error you give mentions, you
can only index arrays by POSITIVE INTEGERS or LOGICALS.

After you sort these two errors out, try debugging the rest for
yourself, as I don't know what you want to do with the other errors
that pop up.

-Nathan
From: TideMan on
On Jan 21, 1:58 pm, Nathan <ngrec...(a)gmail.com> wrote:
> On Jan 20, 4:35 pm, "Sow-Mun Chan" <sow.c...(a)warwick.ac.uk> wrote:
>
>
>
> > Hi, I have found many threads that had the same problem. Unfortunately, I am not that familiar with Matlab so I wasn't able to fix it. My wrote represents the ear, and what the neurons do when a signal gets through - there will be a spike. I have a few loops in it, that count how often a signal gets through, etc. Now I want to draw a period histogram that shows me at what phase of the sine wave the most spikes are.
> > I've written the code and now it tells me "Subscript indices must either be real positive integers or logicals." The error is in the line y = a * sin (x) + b * (randn(1,length(x)));
> > Can anybody help me with this issue?
> > Thank you in advance.
>
> > Sow-Mun
>
> > d = 10;
> > x = 1:(1/d):300;
> > a = 1;
> > b = 2;
> > f = 1000;
> > %f = frequency
>
> > y = a * sin (x) + b * (randn(1,length(x)));
> >     %sine wave with amplitude a, noise with size b  
>
> > count = zeros(1,100);
> > period = zeros(1,60);
>
> > for j = 1:100
>
> >     c=0.002;
> >     %c=refractory period5
> >     k=zeros(1,length(x));
>
> >     %d needed due to 0.1 steps
> >     for i = 1:length(x)
>
> >         c=c+((1/(f*d))/(2*pi));
> >         %time step
>
> >         if y(i)>1  && c>=0.002
>
> >             k(i)=1;
> >             % if y(i)>1 and the refractory period is equal to to or bigger than
> >             % 0.002 then a signal is detected
> >             c=0;
> >         end
>
> >     end
>
> >     c = sum (k);
> >     count(c+1) = count(c+1) + 1;
>
> >     phase = 0;
> >     k = find(k);
> >     %find positions of spikes
> >     for v = 1:length(k)
> >         phase(v) = mod(x(k(v)), 2*pi);
> >         %find x-value at position of spike
> >     end
>
> >     bins = round(phase/(2*pi)*60);
> >     %round the number to integer so they can be allocated to a bin
> >     bins = bins(bins > 0);
> >     %remove all zeros
>
> >     for length = 1:length(bins)
> >         period(bins(length)) = period(bins(length)) + 1;
>
> >     end
>
> > end
>
> >   count
> > time = x*(1/f)/(2*pi);
> > plot (time, y)
> > figure
> > plot (time, k)
> > figure
> > plot(time, y, time, k, 'red' );
> > xlabel('time')
> > ylabel('amplitude')
> > figure
> > plot(period, '.');
>
> Your problem seems that your code is riddled with errors.
>
> One major thing you need to notice:
> You created a variable called length within one of your loops.
> Doing so, you can no longer use the function LENGTH within your mfile.
> NEVER USE FUNCTION NAMES AS VARIABLES.
> I cannot stress that enough.
> Change this, and then go through your code to see what you want done.
>
> Another error I see is that you are trying to index your variable
> count by a non-integer variable c. As the error you give mentions, you
> can only index arrays by POSITIVE INTEGERS or LOGICALS.
>
> After you sort these two errors out, try debugging the rest for
> yourself, as I don't know what you want to do with the other errors
> that pop up.
>
> -Nathan

Nathan: Yes, I think he's defined sin as a variable as well.

One thing to do to get going is to put:
clear all
at the top of your script.
This will remove all the horrors you've introduced in previous runs.