/ concept-collection / seqlab
Sign in
concept-collection / seqlab
seqlab / src / engine / pulseq / +mr / decompressShape.m
69 lines · 2.5 KBCodeBlameHistory
78d04f1seqlab: write and view pulseq MRI sequences in the browserJeremy Magland 1function w = decompressShape(shape, forceDecompression)
2%decompressShape Decompress a gradient or pulse shape.
3% w=decompressShape(shape) Decompress the shape compressed with a run-length
4% compression scheme on the derivative. The given shape is structure with
5% the following fields:
6% num_samples - the number of samples in the uncompressed waveform
7% data - containing the compressed waveform
8%
9% See also compressShape
12dataPack = shape.data;
13dataPackLen = length(dataPack);
14numSamples=shape.num_samples;
16if nargin<2
17 forceDecompression=false;
18end
20if ~forceDecompression && numSamples==dataPackLen
21 % uncompressed shape
22 w=dataPack';
23 return;
24end
26w= zeros(1, numSamples) ; % pre-allocate the result matrix
27 % dimensons: (1,length of the data set)
29% decompression starts here
31dataPackDiff = dataPack(2:end) - dataPack(1:end-1);
33% when dataPackDiff == 0 the subsequent samples are equal ==> marker for
34% repeats (run-length encoding)
35dataPackMarkers=find(dataPackDiff==0.0);
37countPack= 1; % counter 1: points to the current compressed sample
38countUnpack= 1; % counter 2: points to the current uncompressed sample
40for i=1:length(dataPackMarkers)
41 nextPack=dataPackMarkers(i); % careful, this index may have "false positives" , e.g. if the value 3 repeats 3 times, then we will have 3 3 3
42 currUnpackSamples=nextPack-countPack;
43 if currUnpackSamples < 0 % this rejects false positives
44 continue;
45 elseif currUnpackSamples > 0 % do we have an unpacked block to copy?
46 w(countUnpack:(countUnpack+currUnpackSamples-1)) = dataPack(countPack:(nextPack-1));
47 countPack = countPack + currUnpackSamples;
48 countUnpack = countUnpack + currUnpackSamples;
49 end
50 % now comes the packed/repeated section
51 rep=dataPack(countPack+2)+2;
52 w(countUnpack:(countUnpack+rep-1))=dataPack(countPack);
53 countPack= countPack + 3;
54 countUnpack= countUnpack + rep;
55end
57% samples left?
58if (countPack<=dataPackLen)
59 assert(dataPackLen-countPack==numSamples-countUnpack);
60 % copy the rest of the shape, it is unpacked
61 w(countUnpack:end)= dataPack(countPack:end);
62end
64w = cumsum(w);
65w=w(:);
67% % test the new function against the old slow version
68% w1=mr.decompressShape0(shape);
69% assert(all(w==w1));
moveopenescclose