Dls c5w3a2e2

I have been struggling with an error in Course 5 (Sequence Models), Week 3, Assignment 2 (Trigger Word Detection), Exercise 2 - insert_audio_clip.

Here is my error message: ---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
in
19 print(“\033[92m All tests passed!”)
20
—> 21 insert_audio_clip_test(insert_audio_clip)

in insert_audio_clip_test(target)
8 #print(f"xx: {segment_time}“)
9 assert segment_time[0] > 4400, “Error: The audio clip is overlapping with the first segment”
—> 10 assert duration + 1 == len(activates[0]) , “The segment length must match the audio clip length”
11 assert audio_clip != backgrounds[0] , “The audio clip must be different than the pure background”
12 assert segment_time == (7286, 8201), f"Wrong segment. Expected: (7286, 8201) got:{segment_time}”

AssertionError: The segment length must match the audio clip length

I have printed segment_ms - and it is 916. And it have printed segment_time – and the difference between the end and the start is 916.

I am completely stymied. This assignment just doesn’t require that much code, so i am confused what I did wrong. Any help is very appreciated.

As per your error,
I would check first in the previous grader cell exercise 1
UNQ_C1
GRADED FUNCTION: is_overlapping

if my loop over codes are correct as your error tells your segment length doesn’t match with your audio clip length

remember the if statement wants you to mention two conditions
if segment_start is less than or equal to previous_end and
segment_end greater than or equal to previous_start:
then overlap is equal to True
break

Let me know if you recalled this as mentioned.

Then in
UNQ_C2
GRADED FUNCTION: insert_audio_clip
def insert_audio_clip

as segment_ms is length of audio_clip

step 1
basically wants you to create segment_time using the get_random_time_segment from the previous recalled code duration of audio clip in ms

Step 2: Check if the new segment_time overlaps with one of the previous_segments. If so, keep
picking new segment_time at random until it doesn’t overlap. To avoid an endless loop
we retry 5 times(≈ 2 lines)
as mentioned per instruction, you retry for 5 times
checking is_overlapping of segmen_time and previous_segments, and retry is greater than or equal to 0
make sure retry is mentioned with a condition retry-1(I suppose this is where the error is for you.

Regards
DP

Here is my if statement from exercise 1:
if (segment_start <= previous_end and segment_end >= previous_start):
Note that is contains both conditions, and is_overlapping appears to be fine.

The audio clip is 916ms long. I checked using:
segment_ms = len(audio_clip)
print(segment_ms)

As you say, my code fails in the While loop. But i have printed out segment_time, and each is 916ms long. (The print statements are inserted by me for debugging and will be removed for grading.)
while is_overlapping(segment_time, previous_segments) and retry >= 0:
segment_start = random.randint(0, 10000 - segment_ms)
segment_time = (segment_start, segment_start + segment_ms)
print(segment_time)
retry -= 1

I am just so frustrated

I added some print statements to my code and here’s what I get running that test block:

segment_ms 916
inserted at (7286, 8201)
segment_ms 916
 All tests passed!

But it looks like you are writing your own code to generate the random time segment. They gave you that function:

def get_random_time_segment(segment_ms):
    """
    Gets a random time segment of duration segment_ms in a 10,000 ms audio clip.
    
    Arguments:
    segment_ms -- the duration of the audio clip in ms ("ms" stands for "milliseconds")
    
    Returns:
    segment_time -- a tuple of (segment_start, segment_end) in ms
    """
    
    segment_start = np.random.randint(low=0, high=10000-segment_ms)   # Make sure segment doesn't run past the 10sec background 
    segment_end = segment_start + segment_ms - 1
    
    return (segment_start, segment_end)

Notice that your logic does not work the same way theirs does. :scream_cat:

Your missing the step 1 of creating segment time by using the get_random_time_segment from the segment_ms

then in the step 2,
it clearly mentioned retry to try 5 times
so mention retry=5
then the below code line

these two code lines is not required.

Regards
DP