You could probably use unfold directly to create these patches. Have a look at this post for a simple example.
unfold