<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<span style="font-size: 14px;" class="">Hey Stephen,</span>
<div class=""><span style="font-size: 14px;" class=""><br class="">
</span></div>
<div class=""><span style="font-size: 14px;" class="">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 class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg = [];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg.dataset = full_path;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg.continuous =
<span style="color: #a020f0" class="">'yes'</span>;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(34, 139, 34);" class="">
<span style="color: #000000" class=""> </span>%cfg.demean = 'yes';</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(34, 139, 34);" class="">
%cfg.baselinewindow = baselinewindow;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg.lpfilter =
<span style="color: #a020f0" class="">'no'</span>;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg.hpfilter =
<span style="color: #a020f0" class="">'no'</span>;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">cfg.dftfilter =
<span style="color: #a020f0" class="">'yes'</span>;</div>
<p style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 12px;" class="">
     <br class="webkit-block-placeholder">
</p>
<div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">data = ft_preprocessing(cfg);</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg=[];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.dataset=full_path;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.trialdef.prestim=0.175;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.trialdef.poststim=0.600;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.trialdef.eventvalue=[6 7];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.trialdef.eventtype=<span style="color: #a020f0" class="">'STI101'</span>;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg = ft_definetrial(cfg);</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
trl = cfg.trl(:,1:3);</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg = [];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.trl = trl;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
data_segmented = ft_redefinetrial(cfg,data);</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg = [];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.resamplefs = 250;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
data_ds = ft_resampledata(cfg, data_segmented);</div>
</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg = [];</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.channel = <span style="color: #a020f0" class="">'MEGGRAD'</span>;</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
cfg.keeptrials = <span style="color: #a020f0" class="">'yes'</span>; </div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
data_TL = ft_timelockanalysis(cfg,data_ds);</div>
</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Courier;" class="">
plot(data_TL.time,squeeze(mean(data_TL.trial(:,39,:),1))) <span style="color: #228b22" class="">
%17, 23 and 39 are examples of spikes in channels</span></div>
</div>
<div class=""><img apple-inline="yes" id="D42E77C1-9E9F-4DF7-9E5F-813544900E27" width="174" height="130" src="cid:81B23B81-2E1C-4A5E-939F-6E3343FC4409@home" class=""><img apple-inline="yes" id="35E2A693-66FA-4F0A-8590-8D3BA9E2937E" width="175" height="151" src="cid:C209709C-82A9-454F-B83C-EE4534E79468@home" class=""></div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><span style="font-size: 14px;" class="">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 apple-inline="yes" id="694224AC-588D-4B6E-98F8-5AAE543B5A52" src="cid:D20BCD8D-EAA8-4B7A-8B4C-BE46A7FBE18D@home" class=""><img apple-inline="yes" id="92830C4D-85DC-4A86-837D-6ED539088E8F" src="cid:6115EB0D-6852-47F1-849B-CBE23A57FEE5@home" class=""></div>
<div><span style="font-size: 14px;" class="">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;" class=""><br class="">
</span></div>
<div><span style="font-size: 14px;" class="">Any assistance is of course greatly appreciated. Thanks!</span></div>
<div><span style="font-size: 14px;" class=""><br class="">
</span></div>
<div><span style="font-size: 14px;" class="">Benjy</span><br class="">
<blockquote type="cite" class="">
<div class="">On Feb 2, 2021, at 4:14 PM, Stephen Whitmarsh <<a href="mailto:stephen.whitmarsh@gmail.com" class="">stephen.whitmarsh@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">Hey Benjy,
<div class=""><br class="">
</div>
<div class="">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 class=""><br class="">
</div>
<div class="">To avoid this, and to create a fair comparison between your different trial duration segmentation, I would do the following:</div>
<div class=""><br class="">
</div>
<div class="">- 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 class="">- segment your filtered data into trials using ft_redefinetrial.</div>
<div class="">
<div class="">- do your resampling </div>
<div class=""></div>
</div>
<div class="">- check the different triallengths.</div>
<div class=""><br class="">
</div>
<div class="">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 class=""><br class="">
</div>
<div class="">Have fun,<br class="">
</div>
<div class="">Stephen</div>
</div>
<br class="">
<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" class="">benjy.barnett.20@ucl.ac.uk</a>>:<br class="">
</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;" class=""><font face="Courier" class="">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 class=""><font face="Courier" class=""><br class="">
</font></div>
<div class="">
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)" class="">
<font face="Courier" class=""><span style="" class=""><span style="white-space:pre-wrap" class=""></span> </span>% define trials</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.dataset = full_path;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.trialdef.prestim = 0.175; </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.trialdef.poststim = 0.6;
<span style="color:rgb(34,139,34)" class="">% compare with 2.5 seconds</span></font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.trialdef.eventvalue = [6 7 9];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">       </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.trialdef.eventtype =
<span style="color:rgb(160,32,240)" class="">'STI101'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg = ft_definetrial(cfg);</font></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 12px;" class="">
<font face="Courier" class=""> <br class="">
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(34,139,34)" class="">
<font face="Courier" class=""><span style="" class="">        </span>% preprocess</font></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; min-height: 12px;" class="">
<font face="Courier" class="">        <br class="">
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.demean =
<span style="color:rgb(160,32,240)" class="">'yes'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.baselinewindow = [-0.175 0.025];<br class="">
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.lpfilter =
<span style="color:rgb(160,32,240)" class="">'no'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.hpfilter =
<span style="color:rgb(160,32,240)" class="">'no'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.dftfilter =
<span style="color:rgb(160,32,240)" class="">'yes'</span>;</font></div>
</div>
<div class="">
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class=""><span style="white-space:pre-wrap" class=""></span>split_files{1} = ft_preprocessing(cfg);
<span style="color:rgb(34,139,34)" class="">%preprocess</span></font></div>
</div>
<div class=""><span style="color:rgb(34,139,34)" class=""><font face="Courier" class=""><br class="">
</font></span></div>
<div class=""><span style="color:rgb(34,139,34)" class=""><font face="Courier" class=""><br class="">
</font></span></div>
<div class=""><span style="color:rgb(34,139,34)" class=""><font face="Courier" class=""><span style="white-space:pre-wrap" class=""></span>%%%%%%%%%%%Downsampling%%%%%%%%%</font></span></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class=""> 
<span style="white-space:pre-wrap" class=""></span>cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">   
<span style="white-space:pre-wrap" class=""></span>cfg.resamplefs = 250;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">   
<span style="white-space:pre-wrap" class=""></span>data = ft_resampledata(cfg, split_files{1});</font></div>
<div class=""><font face="Courier" class=""><br class="">
</font></div>
<div class=""><font face="Courier" class=""><br class="">
</font></div>
<div class=""><font face="Courier" class=""><span style="white-space:pre-wrap" class=""></span>%%Visualisation</font></div>
<div class=""><font face="Courier" class=""><span style="white-space:pre-wrap" class=""></span>cfg = [];</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.channel =
<span style="color:rgb(160,32,240)" class="">'MEGGRAD'</span>;</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        cfg.keeptrials =
<span style="color:rgb(160,32,240)" class="">'yes'</span>; </font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        data = ft_timelockanalysis(cfg,data);</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class=""><span style="white-space:pre-wrap" class=""></span>Xlim([-0.2 0.6])</font></div>
<div class="">
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">        plot(data.time,squeeze(mean(data.trial(:,39,:),1))) </font></div>
</div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class=""><br class="">
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">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" class=""><font face="Courier" class=""><br class="">
</font></div>
<div style="margin:0px;font-stretch:normal;line-height:normal" class=""><font face="Courier" class="">Thanks</font></div>
<div class=""><span id="cid:177637d632ea17411e71"><2_5_ch39_full.png></span><span id="cid:177637d632eb90abab52"><600_ch39_full.png></span></div>
</div>
_______________________________________________<br class="">
fieldtrip mailing list<br class="">
<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" originalsrc="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" shash="vptWUWFHIO7XTZ7KaH7W0nanzqoEiCEqRBRMY3ORkqtc8VkBS4OqLT41pWVwlpBFoo2vQfoAlCl0JxstaOady8vS+ov9VC5WKhSkLGkPH7huWBln0XFXi8qVnDouFEk+vFAdSVvdDIZj2N+DsRkmqPi+7zQHlw5Oe6P77eirC7Y=" rel="noreferrer" target="_blank" class="">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br class="">
<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" originalsrc="https://doi.org/10.1371/journal.pcbi.1002202" shash="opeJqJpODKjdv0bM7dMvc6a33uXEQJ7qatUiZEibwyQOAm+WnR4lIZvfa7PL7Z5RtrsE7aKo9fK3Tho57L45KERUxYS0J4uaxAIWU1AgqRuvUhbU9gk3Apg2Mn6IgiLEvios7SRWzrBMTm5XSHYfEcDrgpcGBhQVcZj84PhMKRo=" rel="noreferrer" target="_blank" class="">https://doi.org/10.1371/journal.pcbi.1002202</a><br class="">
</blockquote>
</div>
_______________________________________________<br class="">
fieldtrip mailing list<br class="">
<a href="https://mailman.science.ru.nl/mailman/listinfo/fieldtrip" class="">https://mailman.science.ru.nl/mailman/listinfo/fieldtrip</a><br class="">
https://doi.org/10.1371/journal.pcbi.1002202<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>