<div dir="ltr">Hi Benjy,<div><br></div><div>Just a though - but am I right that you are plotting an average over channels? Can you look at the channels individually and see on which channels the jumps occur? Might you accidentally be adding a reference/external/trigger channel to the average?</div><div><br></div><div>Cheers,</div><div>Stephen</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Op wo 3 feb. 2021 om 18:42 schreef Barnett, Benjy <<a href="mailto:benjy.barnett.20@ucl.ac.uk">benjy.barnett.20@ucl.ac.uk</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div style="overflow-wrap: break-word;">
<span style="font-size:14px">Hey Stephen,</span>
<div><span style="font-size:14px"><br>
</span></div>
<div><span style="font-size:14px">Thanks for your help! Whilst this certainly answers my question about the slight difference between different trial lengths, the huge spikes at the beginning of trials remains regardless of whether I filter
 the data as a whole first, or if I segment into trials and then filter. Here’s my code for doing the filtering first (my code for the alternative way is in my initial post):</span></div>
<div><br>
</div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg = [];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg.dataset = full_path;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg.continuous =
<span style="color:rgb(160,32,240)">'yes'</span>;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)">
<span style="color:rgb(0,0,0)"> </span>%cfg.demean = 'yes';</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)">
%cfg.baselinewindow = baselinewindow;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg.lpfilter =
<span style="color:rgb(160,32,240)">'no'</span>;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg.hpfilter =
<span style="color:rgb(160,32,240)">'no'</span>;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal">cfg.dftfilter =
<span style="color:rgb(160,32,240)">'yes'</span>;</div>
<p style="margin:0px;font-stretch:normal;line-height:normal;min-height:12px">
     <br>
</p>
<div style="margin:0px;font-stretch:normal;line-height:normal">data = ft_preprocessing(cfg);</div>
<div><br>
</div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg=[];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.dataset=full_path;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.trialdef.prestim=0.175;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.trialdef.poststim=0.600;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.trialdef.eventvalue=[6 7];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.trialdef.eventtype=<span style="color:rgb(160,32,240)">'STI101'</span>;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg = ft_definetrial(cfg);</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
trl = cfg.trl(:,1:3);</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier;min-height:13px">
<br>
</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg = [];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.trl = trl;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
data_segmented = ft_redefinetrial(cfg,data);</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier;min-height:13px">
<br>
</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg = [];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.resamplefs = 250;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
data_ds = ft_resampledata(cfg, data_segmented);</div>
</div>
<div><br>
</div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg = [];</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.channel = <span style="color:rgb(160,32,240)">'MEGGRAD'</span>;</div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
cfg.keeptrials = <span style="color:rgb(160,32,240)">'yes'</span>; </div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
data_TL = ft_timelockanalysis(cfg,data_ds);</div>
</div>
<div><br>
</div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Courier">
plot(data_TL.time,squeeze(mean(data_TL.trial(:,39,:),1))) <span style="color:rgb(34,139,34)">
%17, 23 and 39 are examples of spikes in channels</span></div>
</div>
<div><img id="gmail-m_-3919107561692488162D42E77C1-9E9F-4DF7-9E5F-813544900E27" width="174" height="130" src="cid:177690f1a975b206ef61"><img id="gmail-m_-391910756169248816235E2A693-66FA-4F0A-8590-8D3BA9E2937E" width="175" height="151" src="cid:177690f1a975b2e86772"></div>
<div><br>
</div>
<div><br>
</div>
<div><span style="font-size:14px">I’m still seeing spikes as in the images attached. Something else strange: when I change the poststim parameter to 0.075 instead of 0.175 (keeping everything else equal), the direction of the spike completely
 switches (see third image attached). This happens regardless or ft_definetrial/preprocessing order. Moreover, when I look at the signal across the single trial encapsulating all the data (i.e. before segmentation), there are none of these huge spikes where
 every event would be, only a few spikes that I assume are SQUID jumps.</span></div>
<div><img id="gmail-m_-3919107561692488162694224AC-588D-4B6E-98F8-5AAE543B5A52" src="cid:177690f1a977e5fd32b3"><img id="gmail-m_-391910756169248816292830C4D-85DC-4A86-837D-6ED539088E8F" src="cid:177690f1a974341a9734"></div>
<div><span style="font-size:14px">This makes me think the issue is coming at the ft_definetrial() step. But since this does not read in the actual MEG data, I can’t work out what is causing this effect, it certainly doesn’t seem to be an issue with
 my data. I’ve added a couple of lines to the ft_trialfun_general function, so to be sure I’ve removed them and am just using the default version yet the problem is persisting. </span></div>
<div><span style="font-size:14px"><br>
</span></div>
<div><span style="font-size:14px">Any assistance is of course greatly appreciated. Thanks!</span></div>
<div><span style="font-size:14px"><br>
</span></div>
<div><span style="font-size:14px">Benjy</span><br>
<blockquote type="cite">
<div>On Feb 2, 2021, at 4:14 PM, Stephen Whitmarsh <<a href="mailto:stephen.whitmarsh@gmail.com" target="_blank">stephen.whitmarsh@gmail.com</a>> wrote:</div>
<br>
<div>
<div dir="ltr">Hey Benjy,
<div><br>
</div>
<div>I would suspect this has to do with your (dft) filtering, which depends on the amount of data, and perhaps some small numerical differences due to resampling. Strange jumps at onset/offset of data segments also often indicate something to do with
 filtering.</div>
<div><br>
</div>
<div>To avoid this, and to create a fair comparison between your different trial duration segmentation, I would do the following:</div>
<div><br>
</div>
<div>- preprocess all your data as one long 'trial'. I.e. without segmentation. Do all your filtering at this step, i.e. at one go over all your data. This gives you the maximum frequency resolution & ability to filter low frequencies.</div>
<div>- segment your filtered data into trials using ft_redefinetrial.</div>
<div>
<div>- do your resampling </div>
<div></div>
</div>
<div>- check the different triallengths.</div>
<div><br>
</div>
<div>If anything stays slightly different, I would imagine it has to do with the resampling, because samples will end up slightly different on your axis, creating numerical differences. You could resample before segmenting, which would be more efficient,
 but then you'll have to deal with changing timing of your markers which would still be in the original samplerate. Dealing with time-axis and samples is a pain so personally I therefore always let FT deal with the resampling after trial segmentation.</div>
<div><br>
</div>
<div>Have fun,<br>
</div>
<div>Stephen</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Op di 2 feb. 2021 om 16:36 schreef Barnett, Benjy <<a href="mailto:benjy.barnett.20@ucl.ac.uk" target="_blank">benjy.barnett.20@ucl.ac.uk</a>>:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><font face="Courier">Hey. When I am preprocessing my MEG neuromag data, I’m noticing that the signal of my channels are altered depending on how long after the stimulus I include in the trial, even in
 the portion of the trial that is overlapping between the two cases. For instance, if I use cfg.poststim = 2.5 in one run and cfg.poststim = 0.6 in a second, the two signals are not identical up until 0.6 seconds. Please see my code and attached plots of a
 Grad sensor in these two cases up to 0.6 seconds.</font>
<div><font face="Courier"><br>
</font></div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)">
<font face="Courier"><span><span style="white-space:pre-wrap"></span> </span>% define trials</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.dataset = full_path;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.trialdef.prestim = 0.175; </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.trialdef.poststim = 0.6;
<span style="color:rgb(34,139,34)">% compare with 2.5 seconds</span></font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.trialdef.eventvalue = [6 7 9];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">       </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.trialdef.eventtype =
<span style="color:rgb(160,32,240)">'STI101'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg = ft_definetrial(cfg);</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal;min-height:12px">
<font face="Courier"> <br>
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)">
<font face="Courier"><span>        </span>% preprocess</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal;min-height:12px">
<font face="Courier">        <br>
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.demean =
<span style="color:rgb(160,32,240)">'yes'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.baselinewindow = [-0.175 0.025];<br>
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.lpfilter =
<span style="color:rgb(160,32,240)">'no'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.hpfilter =
<span style="color:rgb(160,32,240)">'no'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.dftfilter =
<span style="color:rgb(160,32,240)">'yes'</span>;</font></div>
</div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier"><span style="white-space:pre-wrap"></span>split_files{1} = ft_preprocessing(cfg);
<span style="color:rgb(34,139,34)">%preprocess</span></font></div>
</div>
<div><span style="color:rgb(34,139,34)"><font face="Courier"><br>
</font></span></div>
<div><span style="color:rgb(34,139,34)"><font face="Courier"><br>
</font></span></div>
<div><span style="color:rgb(34,139,34)"><font face="Courier"><span style="white-space:pre-wrap"></span>%%%%%%%%%%%Downsampling%%%%%%%%%</font></span></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier"> 
<span style="white-space:pre-wrap"></span>cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">   
<span style="white-space:pre-wrap"></span>cfg.resamplefs = 250;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">   
<span style="white-space:pre-wrap"></span>data = ft_resampledata(cfg, split_files{1});</font></div>
<div><font face="Courier"><br>
</font></div>
<div><font face="Courier"><br>
</font></div>
<div><font face="Courier"><span style="white-space:pre-wrap"></span>%%Visualisation</font></div>
<div><font face="Courier"><span style="white-space:pre-wrap"></span>cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.channel =
<span style="color:rgb(160,32,240)">'MEGGRAD'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        cfg.keeptrials =
<span style="color:rgb(160,32,240)">'yes'</span>; </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        data = ft_timelockanalysis(cfg,data);</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier"><span style="white-space:pre-wrap"></span>Xlim([-0.2 0.6])</font></div>
<div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">        plot(data.time,squeeze(mean(data.trial(:,39,:),1))) </font></div>
</div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier"><br>
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">As you can see from the plots attached, these signals are not identical when they should be (shouldn’t they?). The first plot is when I Use 2.5 seconds post stim
 and the second is using 0.6. I’ve altered the xlim to show only up to 600ms on both. Also, can anyone explain these huge spikes at the beginning of trials, that’s my next problem to solve.</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier"><br>
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal"><font face="Courier">Thanks</font></div>
<div><span id="gmail-m_-3919107561692488162cid:177637d632ea17411e71"><2_5_ch39_full.png></span><span id="gmail-m_-3919107561692488162cid:177637d632eb90abab52"><600_ch39_full.png></span></div>
</div>
_______________________________________________<br>
fieldtrip mailing list<br>
<a href="https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.science.ru.nl%2Fmailman%2Flistinfo%2Ffieldtrip&data=04%7C01%7C%7Cd5bce0b0bd7d42360fe308d8c798fa4e%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637478807143731653%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=FF%2FVokvcknAhcAfE3jyxVVZtY7kWUOQlmWlySFw12Rs%3D&reserved=0" rel="noreferrer" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<a href="https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdoi.org%2F10.1371%2Fjournal.pcbi.1002202&data=04%7C01%7C%7Cd5bce0b0bd7d42360fe308d8c798fa4e%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637478807143731653%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=P1qCeEY18OkQ8BJZ8yY4LX56%2Brg19SUOHOrPq%2BaAxlM%3D&reserved=0" rel="noreferrer" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br>
</blockquote>
</div>
_______________________________________________<br>
fieldtrip mailing list<br>
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<a href="https://doi.org/10.1371/journal.pcbi.1002202" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br>
</div>
</blockquote>
</div>
<br>
</div>
</div>

_______________________________________________<br>
fieldtrip mailing list<br>
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" rel="noreferrer" target="_blank">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br>
<a href="https://doi.org/10.1371/journal.pcbi.1002202" rel="noreferrer" target="_blank">https://doi.org/10.1371/journal.pcbi.1002202</a><br>
</blockquote></div>