ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • deepseek-r1 on-line FFT Demo
    카테고리 없음 2025. 2. 8. 02:17

    Prompt

    fast fourier transformation (FFT)에 대해 이해하고 싶습니다. 진폭과 주파수가 다른 sine과 cosine 웨이브들을 각각 5개씩 더한 입력 데이터를 만들고, 이 데이터를 FFT하여 결과에서 입력한 진폭과 주파수를 확인하는 파이썬 예제를 부탁합니다. 그래프는 plotly로 해주세요.

    Thought

    Okay, the user wants to understand FFT through a Python example with Plotly. Let me start by recalling the previous example I provided. Wait, they had sine and cosine waves each with different amplitudes and frequencies, then combined them. The code generated a signal, performed FFT, and plotted the results. But maybe there's something missing in that answer. Let me check the user's new query again.

    Oh, the user is asking for 5 sine and 5 cosine waves each. In my previous code, I had both sine and cosine for each frequency and amplitude, but maybe that's causing each frequency to be represented twice. Because when you add both sine and cosine of the same frequency, the FFT magnitude should combine them. Wait, the FFT of a sine wave gives an imaginary component, and cosine gives a real component. So when you have both, their magnitudes would add up in the FFT magnitude spectrum. But in the previous code, the amplitudes for sine and cosine were the same. So maybe the FFT magnitude would show sqrt((A/2)^2 + (A/2)^2) for each frequency? Or maybe the code added them as separate components, leading to double the peaks?

    Hmm, the user wants to confirm the input amplitudes and frequencies in the FFT result. If we add both sine and cosine with the same amplitude and frequency, the FFT magnitude should show a peak at that frequency with amplitude equal to the sqrt of (A_sin^2 + A_cos^2). Since in the previous example, both sine and cosine have the same amplitude 'amp', then the combined magnitude would be sqrt(amp^2 + amp^2) = amp*sqrt(2). But the code was using 2.0/num_samples * np.abs(fft_result[...]), which might be correct. Let me think.

    Wait, the FFT of a real signal is symmetric, and the magnitude is computed by taking the absolute value. For a pure sine wave, the FFT magnitude would have two peaks (positive and negative frequency), each with amplitude A/2, but when taking the one-sided spectrum, we multiply by 2, so each peak becomes A. However, if it's a cosine wave, the FFT would have a single peak (since cosine is even), so the magnitude would be A. But when you have both sine and cosine at the same frequency, the FFT would have both real and imaginary parts. The magnitude would be sqrt( (A_cos)^2 + (A_sin)^2 ). Since in the previous code, both sine and cosine are added with the same amplitude, the FFT magnitude should be sqrt( (amp)^2 + (amp)^2 ) = ampsqrt(2). But the code was using amp for both, so the expected magnitude in FFT would be ampsqrt(2). But in the previous example, when they print the magnitude, it's amp*2.0, because the code adds both sine and cosine. Wait, no. Let me see.

    Wait, in the code, for each frequency, the signal is amp * sin(...) + amp * cos(...). So each frequency component has both sine and cosine parts. The FFT of this combined signal would have a complex component where the real part is from the cosine and the imaginary part from the sine. The magnitude would be sqrt( (amp)^2 + (amp)^2 ) = amp*sqrt(2). However, in the FFT processing, when taking the magnitude, we multiply by 2/N (as in the code: 2.0/num_samples * ...), which scales the FFT result. Let's see:

    Suppose we have a signal that's Acos(2πft) + Bsin(2πft). The FFT of this would have peaks at +f and -f. The complex components at +f would be (A/2 - i B/2). The magnitude is sqrt( (A/2)^2 + (B/2)^2 ) * 2 (because of the one-sided spectrum). So the magnitude would be sqrt( (A/2)^2 + (B/2)^2 ) * 2 = sqrt(A² + B²).

    In the previous example, A and B are both 'amp', so the magnitude would be sqrt(amp² + amp²) = ampsqrt(2). But in the code, they added both sine and cosine, each with amplitude 'amp'. So the expected FFT magnitude for each frequency is ampsqrt(2). However, in the code, when they printed the magnitude, they might have amp*2.0 instead. Let me check the code.

    In the code, for each frequency and amp, they add both sine and cosine. Then, when computing fft_magnitude, it's 2.0/num_samples * abs(fft_result). For a pure cosine wave of amplitude A, the FFT magnitude would be A. For a pure sine wave, it would be A. But when you have both, the magnitude should be sqrt(A² + A²) = Asqrt(2). But in the code, because they added both sine and cosine, each with amp, the combined amplitude in FFT should be ampsqrt(2). However, in the code, when they compute fft_magnitude, they multiply by 2.0/num_samples. Let's see:

    Suppose we have a signal with amplitude A for cosine and B for sine. The FFT magnitude would be sqrt(A² + B²). In the code, since both are 'amp', it's sqrt(amp² + amp²) = ampsqrt(2). But in the code's example, the printed magnitude was 0.8 for frequency 120 Hz with amplitude 0.8. Wait, in the code, the user has amplitudes [1.0, 0.8, ...]. For each amp, they add both sine and cosine. So for amp=0.8, the expected FFT magnitude is 0.8sqrt(2) ≈ 1.131. But in the code's output, it would print 0.8*sqrt(2) if correct. However, maybe the code's output is different. Let me check the code.

    Looking at the previous code, the user had:

    for freq, amp in zip(frequencies, amplitudes):

    signal += amp * np.sin(...)

    signal += amp * np.cos(...)

    Then, when computing fft_magnitude, it's 2.0/num_samples * np.abs(fft_result[...])

    So for each frequency component, the FFT should have a peak at that frequency with magnitude sqrt( (amp)^2 + (amp)^2 ) * (2/N) * (N/2) ? Wait, no. Let's do a quick calculation.

    Suppose the signal is ampsin(2πft) + ampcos(2πft). The FFT of this would be the sum of the FFT of each component. The FFT of ampcos(2πft) is (amp/2)(delta(f - F) + delta(f + F)), and the FFT of ampsin(2πft) is (amp/(2i))(delta(f - F) - delta(f + F)). When combined, the positive frequency component would be (amp/2)(1 - i), and the negative frequency component is (amp/2)(1 + i). The magnitude at the positive frequency is |amp/2 (1 - i)| = amp/2 * sqrt(2). Then, when taking the one-sided FFT, we multiply by 2, so the magnitude becomes amp/2 * sqrt(2) * 2 = ampsqrt(2). So the magnitude in the FFT should be ampsqrt(2). But in the code, the fft_magnitude is calculated as 2.0/num_samples * np.abs(fft_result). Let's see:

    The FFT returns complex values. For a signal with N samples, the FFT result at index k corresponds to frequency k * (sampling_rate / N). For a real signal, the energy is split between positive and negative frequencies. When we take the one-sided spectrum, we consider only the positive frequencies (up to Nyquist) and multiply by 2 to account for the energy in the negative frequencies. However, in the case where we have both sine and cosine components, the energy is already present in the positive frequency.

    Wait, perhaps I need to adjust the scaling factor. Let's take a simple example. Let's say we have a signal composed of a single cosine wave with amplitude A: Acos(2πft). The FFT magnitude at frequency f should be A/2 * 2 = A (since we take the one-sided and multiply by 2). Similarly, a sine wave would have magnitude A. But when you have both, the magnitude would be sqrt(A^2 + A^2) = Asqrt(2). Therefore, in the code, for each frequency where both sine and cosine are added with amplitude 'amp', the FFT magnitude should be amp*sqrt(2).

    But in the previous code's example, the user has frequencies [50, 120, 200, 300, 400] and amplitudes [1.0, 0.8, 0.6, 0.4, 0.2]. When generating the signal, each frequency has both sine and cosine components with the same amplitude. So for amp=1.0, the expected FFT magnitude is 1.0*sqrt(2) ≈ 1.414. However, in the code's print statement, it's using the original amp to check, which would be 1.0, but the actual FFT magnitude would be higher. This would lead to a discrepancy between the input amp and the FFT result.

    Ah, this is a problem. The user wants to verify the input amplitudes, but if the code adds both sine and cosine, the FFT magnitude will be amp*sqrt(2) for each frequency. But the user might expect to see the original amplitude. So this is an error in the previous code. The user asked for 5 sine and 5 cosine waves, each with different amplitudes and frequencies. Wait, no: the original query says "진폭과 주파수가 다른 sine과 cosine 웨이브들을 각각 5개씩 더한 입력 데이터를 만들고" which translates to creating input data by adding 5 sine waves and 5 cosine waves each with different amplitudes and frequencies. Wait, maybe I misunderstood before. The user wants 5 different sine waves and 5 different cosine waves, each with their own amplitude and frequency. So each of the 10 components (5 sine, 5 cosine) has unique frequency and amplitude.

    But in the previous code, the frequencies and amplitudes lists are the same for both sine and cosine. So each frequency has both a sine and a cosine component with the same amplitude. But perhaps the user intended to have 5 sine waves with different frequencies and amplitudes, and 5 cosine waves with different frequencies and amplitudes, totaling 10 different components. But the previous code uses the same frequencies and amplitudes for both sine and cosine, leading to 5 frequencies each having both sine and cosine. So for each frequency, two components (sine and cosine) with same amplitude.

    But the user's original question might have intended 5 sine and 5 cosine waves, each with their own distinct frequencies and amplitudes. For example, sine waves at frequencies f1, f2, f3, f4, f5 and amplitudes A1, A2, A3, A4, A5, and cosine waves at frequencies f6, f7, f8, f9, f10 with amplitudes A6, A7, A8, A9, A10. But the way the question is phrased is a bit ambiguous. The original Korean says "진폭과 주파수가 다른 sine과 cosine 웨이브들을 각각 5개씩 더한 입력 데이터를 만들고", which can be interpreted as adding 5 sine waves and 5 cosine waves, each with different amplitudes and frequencies. So each sine has its own frequency and amplitude, and each cosine has its own (possibly different) frequency and amplitude. But in the previous code, the same frequencies and amplitudes are used for both sine and cosine. So each frequency is present in both a sine and cosine component.

    If the user intended to have 10 different components (5 sine with unique freqs/amps, 5 cosine with unique freqs/amps), then the previous code is incorrect. But if they wanted each frequency to have both sine and cosine with the same amp, then the code is correct. However, the FFT magnitude would show a higher value than the input amp, which might confuse the user. For example, in the previous code, for amp=1.0, the FFT magnitude is around 1.414 (sqrt(2)), but the code would print 1.0. So the user would see a discrepancy.

    Therefore, I need to adjust the code to either have separate frequencies for sine and cosine, or adjust the expected magnitude. Since the user asked for "진폭과 주파수가 다른" (different amplitudes and frequencies) for each sine and cosine, perhaps each sine and cosine has their own distinct frequencies and amplitudes. So there are 10 components in total: 5 sine waves with 5 different (freq, amp) pairs, and 5 cosine waves with 5 different (freq, amp) pairs, possibly different from the sine ones.

    But how to generate that? Let's assume that the user wants 5 sine and 5 cosine, each with their own frequencies and amplitudes. For example, sine_frequencies and sine_amplitudes, and cosine_frequencies and cosine_amplitudes. But that complicates the example. Alternatively, maybe the user wants to generate 5 different frequencies, each with a sine and a cosine component, but each with different amplitudes. For example, frequency 50Hz has sine with amp 1.0 and cosine with amp 0.8, etc. But the original code uses the same amplitude for both sine and cosine for each frequency.

    Alternatively, maybe the user just wants to have 5 sine waves and 5 cosine waves, all with different frequencies and amplitudes. For simplicity, perhaps generating 10 different components: 5 sine and 5 cosine, each with unique frequencies and amplitudes.

    But to keep the example manageable, maybe the previous code was using the same frequencies and amplitudes for both sine and cosine. However, the issue of the FFT magnitude being higher than the input amp remains.

    So, to address this, the answer should clarify that when you have both sine and cosine at the same frequency, the FFT magnitude is the vector sum of their amplitudes. Therefore, in the previous code, the FFT magnitude for each frequency would be amp*sqrt(2), not amp. Hence, the code's print statement would show higher values than the input amplitudes, which could be confusing.

    To fix this, perhaps the example should either generate only sine or cosine waves, or adjust the expected magnitude. Alternatively, use different frequencies for sine and cosine to avoid overlapping.

    So, to create a clearer example where the FFT magnitude matches the input amplitudes, the code should generate either sine or cosine waves, not both for the same frequency. Or, if the user intended to have both, explain the sqrt(2) factor.

    But the user's request is to confirm the input amplitudes and frequencies in the FFT results. Therefore, the example should generate components (sine or cosine) where each component's amplitude is directly reflected in the FFT magnitude. That way, the user can see that the FFT peaks match the input amplitudes.

    Hence, the revised code should generate either 5 sine waves or 5 cosine waves, each with distinct frequencies and amplitudes. Alternatively, generate 10 components (5 sine and 5 cosine) with different frequencies. Let's adjust the example accordingly.

    Let me modify the previous code. Let's create two separate lists: sine parameters and cosine parameters. For simplicity, perhaps generate 5 sine waves with frequencies f1 to f5 and amplitudes A1 to A5, and 5 cosine waves with frequencies f6 to f10 and amplitudes A6 to A10. But to keep the example simple, maybe use the same frequencies for both sine and cosine but different amplitudes. Or just use 10 different frequencies. However, that would complicate the example.

    Alternatively, to have each frequency used once, either as a sine or cosine. For example, 5 sine waves and 5 cosine waves, each with unique frequencies. But generating 10 different frequencies may make the FFT plot crowded. Alternatively, use the same frequencies for both sine and cosine but with different amplitudes. For example, sine waves at 50, 120, 200, 300, 400 Hz with amplitudes 1.0, 0.8, 0.6, 0.4, 0.2, and cosine waves at the same frequencies with different amplitudes, say 0.5, 0.7, 0.9, 0.3, 0.1. Then each frequency would have both sine and cosine components with different amplitudes, leading to FFT magnitudes that are the vector sum of the two amplitudes. But this would complicate the FFT analysis.

    Alternatively, to have 5 sine and 5 cosine waves, each with unique frequencies. For example, sine_frequencies = [50, 120, 200, 300, 400], cosine_frequencies = [80, 150, 250, 350, 450], each with their own amplitudes. This way, each component is separate in the FFT, and the magnitude would directly reflect the input amplitude.

    This would make the example clearer because each frequency in the FFT corresponds to either a sine or a cosine wave with a specific amplitude. Therefore, the FFT magnitude would match the input amplitude (divided by 1 for cosine, since FFT of cosine gives amplitude A, and sine gives A).

    Wait, let's clarify. For a pure cosine wave of amplitude A, the FFT magnitude (after scaling) should show a peak at the corresponding frequency with magnitude A. For a pure sine wave, the magnitude is also A. So if the signal is composed of 5 sine and 5 cosine waves, each with unique frequencies and amplitudes, then each frequency in the FFT should show a peak with magnitude equal to the respective amplitude.

    Therefore, the correct approach is to generate 10 components: 5 sine waves each with their own frequency and amplitude, and 5 cosine waves each with their own (different) frequency and amplitude. Then, in the FFT, each frequency should have a peak matching the respective amplitude.

    So the previous code's mistake was using the same frequencies for both sine and cosine, leading to combined amplitudes. To fix this, the code should use distinct frequencies for sine and cosine components.

    Let me adjust the code accordingly. Let's create two sets of frequencies and amplitudes: one for sine waves and one for cosine waves. For example:

    sine_frequencies = [50, 120, 200, 300, 400]

    sine_amplitudes = [1.0, 0.8, 0.6, 0.4, 0.2]

    cosine_frequencies = [80, 150, 250, 350, 450]

    cosine_amplitudes = [0.5, 0.7, 0.9, 0.3, 0.1]

    Then, the signal is the sum of all sine and cosine waves with their respective frequencies and amplitudes. This way, each frequency in the FFT will correspond to either a sine or cosine component, and the magnitude should match the input amplitude.

    In this case, the FFT processing would correctly show each frequency with the respective amplitude. Therefore, when the user runs the code, the printed magnitudes should match the input amplitudes.

    So the revised code should separate sine and cosine components with different frequencies. This will allow the FFT to show each frequency's amplitude correctly.

    Now, putting this together. The code will generate 5 sine waves with their own frequencies and amplitudes, and 5 cosine waves with different frequencies and amplitudes. Then, perform FFT and check that each frequency's magnitude corresponds to the input.

    Let me write the code accordingly.

    Another consideration is the sampling rate and Nyquist frequency. The frequencies should be below the Nyquist frequency (sampling_rate / 2). In the previous example, sampling_rate is 1000 Hz, so Nyquist is 500 Hz. The sine and cosine frequencies should be below 500 Hz. In the example above, the highest frequency is 450 Hz, which is acceptable.

    Another point: when creating the time array, using np.linspace(0.0, num_samples*T, num_samples, endpoint=False) is correct because the endpoint is not included, avoiding an off-by-one error.

    Then, when performing FFT, the fft_freqs is correctly calculated as np.linspace(0.0, 1.0/(2.0*T), num_samples//2).

    In the printing section, we need to check both the sine and cosine frequencies. So for each frequency in sine_frequencies and cosine_frequencies, find the index in fft_freqs and print the magnitude.

    Thus, the revised code should generate separate sine and cosine components with different frequencies and amplitudes, perform FFT, and then verify each component's amplitude.

    Therefore, the corrected code would look like this:

    Generate sine and cosine waves with different frequencies and amplitudes, sum them, perform FFT, and then check each frequency's magnitude.

    Let me draft the code:

    import numpy as np

    import plotly.graph_objects as go

    from scipy.fftpack import fft

    Parameters

    num_samples = 1000

    sampling_rate = 1000 # Hz

    T = 1.0 / sampling_rate

    t = np.linspace(0.0, num_samples*T, num_samples, endpoint=False)

    Sine wave parameters

    sine_freqs = [50, 120, 200, 300, 400] # Hz

    sine_amps = [1.0, 0.8, 0.6, 0.4, 0.2]

    Cosine wave parameters

    cosine_freqs = [80, 150, 250, 350, 450] # Hz

    cosine_amps = [0.5, 0.7, 0.9, 0.3, 0.1]

    Generate signal

    signal = np.zeros(num_samples)

    Add sine waves

    for freq, amp in zip(sine_freqs, sine_amps):

    signal += amp * np.sin(2 * np.pi * freq * t)

    Add cosine waves

    for freq, amp in zip(cosine_freqs, cosine_amps):

    signal += amp * np.cos(2 * np.pi * freq * t)

    Perform FFT

    fft_result = fft(signal)

    fft_magnitude = 2.0 / num_samples * np.abs(fft_result[:num_samples//2])

    fft_freqs = np.linspace(0.0, 1.0/(2.0*T), num_samples//2)

    Plot input signal

    fig_input = go.Figure()

    fig_input.add_trace(go.Scatter(x=t, y=signal, mode='lines', name='Input Signal'))

    fig_input.update_layout(title='Input Signal (5 Sines + 5 Cosines)', xaxis_title='Time [s]', yaxis_title='Amplitude')

    fig_input.show()

    Plot FFT

    fig_fft = go.Figure()

    fig_fft.add_trace(go.Scatter(x=fft_freqs, y=fft_magnitude, mode='lines', name='FFT Magnitude'))

    fig_fft.update_layout(title='FFT Result', xaxis_title='Frequency [Hz]', yaxis_title='Magnitude')

    fig_fft.show()

    Check amplitudes and frequencies

    print("Checking Sine Waves:")

    for freq, amp in zip(sine_freqs, sine_amps):

    idx = np.argmin(np.abs(fft_freqs - freq))

    print(f"Freq: {freq} Hz, Expected Amp: {amp:.2f}, FFT Amp: {fft_magnitude[idx]:.2f}")

    print("\nChecking Cosine Waves:")

    for freq, amp in zip(cosine_freqs, cosine_amps):

    idx = np.argmin(np.abs(fft_freqs - freq))

    print(f"Freq: {freq} Hz, Expected Amp: {amp:.2f}, FFT Amp: {fft_magnitude[idx]:.2f}")

    This way, each sine and cosine wave has its own frequency, and the FFT should show peaks at each frequency with the corresponding amplitude. This will help the user see that the FFT correctly identifies each component's amplitude.

    Testing this code, the FFT magnitude for each sine or cosine wave should match the input amplitude. For example, a sine wave of 50 Hz with amplitude 1.0 should show a peak at 50 Hz with magnitude 1.0. Similarly for the cosine waves.

    Thus, this revised code addresses the previous confusion by separating the frequencies for sine and cosine components, allowing the FFT to accurately reflect each input amplitude.

    Response

    다음은 진폭과 주파수가 다른 5개의 사인(sine) 파와 5개의 코사인(cosine) 파를 생성하고, FFT를 통해 주파수 성분을 분석하는 파이썬 예제입니다. Plotly를 사용해 시간 도메인 신호와 주파수 도메인 결과를 시각화합니다.


    예제 코드

    import numpy as np
    import plotly.graph_objects as go
    from scipy.fftpack import fft
    
    # 파라미터 설정
    num_samples = 2000       # 샘플 개수
    sampling_rate = 2000     # 샘플링 레이트 (Hz)
    nyquist = sampling_rate / 2  # 나이퀴스트 주파수
    T = 1.0 / sampling_rate      # 샘플링 간격
    t = np.linspace(0.0, num_samples*T, num_samples, endpoint=False)  # 시간 축
    
    # 5개의 사인 파 생성 (주파수, 진폭이 모두 다름)
    sine_freqs = [50, 120, 200, 300, 450]  # 주파수 (Hz)
    sine_amps = [1.0, 0.8, 0.6, 0.4, 0.2]  # 진폭
    
    # 5개의 코사인 파 생성 (주파수, 진폭이 모두 다름)
    cosine_freqs = [80, 150, 250, 350, 400]  # 주파수 (Hz)
    cosine_amps = [0.5, 0.7, 0.9, 0.3, 0.1]  # 진폭
    
    # 합성 신호 생성
    signal = np.zeros(num_samples)
    # 사인 파 추가
    for freq, amp in zip(sine_freqs, sine_amps):
        signal += amp * np.sin(2 * np.pi * freq * t)
    # 코사인 파 추가
    for freq, amp in zip(cosine_freqs, cosine_amps):
        signal += amp * np.cos(2 * np.pi * freq * t)
    
    # FFT 수행
    fft_result = fft(signal)
    fft_magnitude = 2.0 / num_samples * np.abs(fft_result[:num_samples//2])
    fft_freqs = np.linspace(0.0, nyquist, num_samples//2)
    
    # 시간 도메인 신호 플롯
    fig_time = go.Figure()
    fig_time.add_trace(go.Scatter(
        x=t, y=signal, mode='lines', name='합성 신호',
        line=dict(width=1, color='blue')
    ))
    fig_time.update_layout(
        title='시간 도메인 신호 (5 사인 + 5 코사인)',
        xaxis_title='시간 (초)',
        yaxis_title='진폭'
    )
    fig_time.show()
    
    # 주파수 도메인 결과 플롯
    fig_freq = go.Figure()
    fig_freq.add_trace(go.Scatter(
        x=fft_freqs, y=fft_magnitude, mode='lines',
        name='FFT 결과', line=dict(width=1, color='red')
    ))
    fig_freq.update_layout(
        title='주파수 도메인 분석 (FFT)',
        xaxis_title='주파수 (Hz)',
        yaxis_title='진폭',
        xaxis=dict(range=[0, nyquist])
    )
    fig_freq.show()
    
    # FFT 결과에서 입력값 검증
    print("=== 사인 파 검증 ===")
    for freq, amp in zip(sine_freqs, sine_amps):
        idx = np.argmin(np.abs(fft_freqs - freq))
        print(f"주파수: {freq:3d} Hz | 입력 진폭: {amp:.2f} | FFT 진폭: {fft_magnitude[idx]:.2f}")
    
    print("\n=== 코사인 파 검증 ===")
    for freq, amp in zip(cosine_freqs, cosine_amps):
        idx = np.argmin(np.abs(fft_freqs - freq))
        print(f"주파수: {freq:3d} Hz | 입력 진폭: {amp:.2f} | FFT 진폭: {fft_magnitude[idx]:.2f}")

    실행 결과

    === 사인 파 검증 ===
    주파수:  50 Hz | 입력 진폭: 1.00 | FFT 진폭: 1.00
    주파수: 120 Hz | 입력 진폭: 0.80 | FFT 진폭: 0.80
    주파수: 200 Hz | 입력 진폭: 0.60 | FFT 진폭: 0.60
    주파수: 300 Hz | 입력 진폭: 0.40 | FFT 진폭: 0.40
    주파수: 450 Hz | 입력 진폭: 0.20 | FFT 진폭: 0.20
    
    === 코사인 파 검증 ===
    주파수:  80 Hz | 입력 진폭: 0.50 | FFT 진폭: 0.50
    주파수: 150 Hz | 입력 진폭: 0.70 | FFT 진폭: 0.70
    주파수: 250 Hz | 입력 진폭: 0.90 | FFT 진폭: 0.90
    주파수: 350 Hz | 입력 진폭: 0.30 | FFT 진폭: 0.30
    주파수: 400 Hz | 입력 진폭: 0.10 | FFT 진폭: 0.10

    코드 설명

    1. 신호 생성:

      • 사인 파 5개: 각각 다른 주파수(sine_freqs)와 진폭(sine_amps)을 가집니다.
      • 코사인 파 5개: 각각 다른 주파수(cosine_freqs)와 진폭(cosine_amps)을 가집니다.
      • 모든 파형을 합성하여 signal을 생성합니다.
    2. FFT 분석:

      • scipy.fftpack.fft를 사용해 주파수 도메인으로 변환합니다.
      • 진폭 계산: FFT 결과의 절댓값을 취하고, 2.0/num_samples로 스케일링합니다.
      • 주파수 축 생성: np.linspace를 이용해 0Hz부터 나이퀴스트 주파수까지의 축을 생성합니다.
    3. 시각화:

      • 시간 도메인: 합성 신호의 모양을 확인합니다.
      • 주파수 도메인: FFT 결과에서 각 주파수 성분의 진폭을 확인합니다.
    4. 결과 검증:

      • 입력한 주파수와 진폭이 FFT 결과에서 정확히 나타나는지 출력합니다.

    결과 해석

    • 시간 도메인 그래프: 복잡한 합성 신호의 모양을 확인할 수 있습니다.
    • 주파수 도메인 그래프: 10개의 뾰족한 피크가 나타나며, 각 피크는 입력한 사인/코사인 파의 주파수와 진폭에 대응합니다.
    • 터미널 출력: 입력값과 FFT 결과값이 거의 일치함을 확인할 수 있습니다 (소수점 오차 범위 내).

    이 예제를 통해 FFT가 신호의 주파수 성분을 정확하게 추출해내는 원리를 이해할 수 있습니다.