updated to use sample indexing of envelope for efficiency